Bug 25620 - NSArrayController.NewObject throws InvalidCastException.
Summary: NSArrayController.NewObject throws InvalidCastException.
Status: RESOLVED FIXED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 1.10.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Rolf Bjarne Kvinge [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2014-12-26 20:30 UTC by Jon Goldberger [MSFT]
Modified: 2015-01-06 10:32 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test Project (27.50 KB, application/zip)
2014-12-26 20:30 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jon Goldberger [MSFT] 2014-12-26 20:30:25 UTC
Created attachment 9189 [details]
Test Project

## Description

When using an NSArrayController that is bound in the .XIB file to an NSMutableArray that holds NSObjects, I should be able to get a new object of the type specified in the .XIB file with:

var p = arrayController.NewObject;

but this returns an NSObjectController not an NSObject. 

var p = arrayController.NewObject.Content;

says it will return an NSObject, but the call to arrayController.NewObject causes an exception even without accessing the Content property. IOW this code:

var p = arrayController.NewObject;

causes an InvalidCastException:

>System.InvalidCastException: Cannot cast from source type to destination type.
>  at at (wrapper castclass) object.__castclass_with_cache (object,intptr,intptr) <IL 0x0002c, 0x000d3>
>  at at MonoMac.ObjCRuntime.Runtime.GetNSObject<MonoMac.AppKit.NSObjectController> (intptr) <0x00073>
>  at MonoMac.AppKit.NSObjectController.get_NewObject () [0x00022] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/2ff3e7b7/source/xamcore/src/build/compat/AppKit/NSObjectController.g.cs:454
>  at RaiseMan.MyDocument.btnCreateEmployee (MonoMac.Foundation.NSObject) [0x00052] in /Users/apple/git/BNR/BNR_Cocoa_Book/RaiseMan/RaiseMan/MyDocument.cs:143
>  at at (wrapper dynamic-method) object.[RaiseMan.MyDocument.Void btnCreateEmployee(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <IL 0x00011, 0x0005f>
>  at at (wrapper native-to-managed) object.[RaiseMan.MyDocument.Void btnCreateEmployee(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <IL 0x0006c, 0x00183>
>  at at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00012>
>  at MonoMac.AppKit.NSApplication.Main (string[]) [0x00041] in /Users/builder/data/lanes/xamcore-lion-1.10-branch/2ff3e7b7/source/xamcore/src/AppKit/NSApplication.cs:105
>  at RaiseMan.MainClass.Main (string[]) [0x00007] in /Users/apple/git/BNR/BNR_Cocoa_Book/RaiseMan/RaiseMan/Main.cs:14

## Steps to reproduce

1. Open the attached sample project.
2. Run the project and click the Add Employee button

Expected result: a new item will be added to the NSTableView

Actual result: InvalidCastException is thrown.

## Workaround

Create your own object of the desired type manually. In this case:

Person p = new Person();

To test:

1. Open the attached sample project.
2. Comment out line 142, var p = arrayController.NewObject;
3. Uncomment line 145, Person p = new Person();
4. Run the project and click the Add Employee button.

Expected result: a new item will be added to the NSTableView

Actual result: As expected.

## Version info:

=== Xamarin Studio ===

Version 5.5.4 (build 15)
Installation UUID: 2dc9022f-f9a8-424f-8284-bf224cbbfde0
Runtime:
	Mono 3.10.0 ((detached/92c4884)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000031

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.2    (API level 8)
		2.3    (API level 10)
		3.0    (API level 11)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: 8.4.0.47 (Business Edition)
Hash: 7244769
Branch: 
Build date: 2014-12-11 14:54:30-0500

=== Xamarin.Mac ===

Version: 1.10.0.18 (Business Edition)

=== Build Information ===

Release ID: 505040015
Git revision: f93940a35458a18052f1a25e106e62ca970d9c40
Build date: 2014-11-19 15:32:41-05
Xamarin addins: dc23cbd91a3a0e1d326328e1229e86c942a49ec8

=== Operating System ===

Mac OS X 10.10.1
Darwin Jons-iMac.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 1 Jon Goldberger [MSFT] 2014-12-26 20:45:43 UTC
## Additional note:

I do have an Objective-C implementation of the test project, and in the Objective-C implementation, I can use:

Person *p = [employeeController newObject];

to get a new object. I do understand that I would have to cast the NSObject to a Person object in C#, but this should be no problem as Person inherits from NSObject. The InvalidCastException appears to be internal to the NSArrayController class when it accesses the NewObject property.
Comment 2 Sadik Ali 2014-12-29 03:36:26 UTC
I have checked this issue and able to reproduce the reported behaviour : NSArrayController.NewObject throws InvalidCastException. To reproduce this issue I have followed the steps provided in bug description.
Note: I am able to reproduce this issue after comment in line no 159, 160, 161, 162 in MyDocument.cs as shown in screencast.

Steps I followed:

1. Open the attached sample project.
2. Build the project, it will show build error.
3. comment line no 159, 160, 161, 162 in "MyDocument.cs".
4. Run the project and click the Add Employee button
5. Observed that you will get  following exception:
"System.InvalidCastException: Cannot cast from source type to destination type."

Please let me know is this the same issue experiencing you and I followed the correct steps to reproduce this issue/behaviour, if No then please let me know that what steps is missing/wrong to reproduce his issue ?

I also observed that, Application is working as expected after following the steps in bug description under " To test:"

Screencast:  http://www.screencast.com/t/QmG4oLfyzG
Ide Logs: http://www.screencast.com/t/XSNzw3llPxWZ
Application output: https://gist.github.com/Rajneesh360Logica/07b660a44b92f7f62fee
Exception : https://gist.github.com/Rajneesh360Logica/6ebc930210ac878a92d5

Please let me know if I am missing anything ?

Environment Info:

=== Xamarin Studio ===

Version 5.5.4 (build 15)
Installation UUID: 45555ffd-f3f0-4f29-9543-e369d711ab7d
Runtime:
	Mono 3.10.0 ((detached/92c4884)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000031

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.iOS ===

Version: 8.4.0.47 (Enterprise Edition)
Hash: 7244769
Branch: 
Build date: 2014-12-11 14:54:30-0500

=== Xamarin.Android ===

Version: 4.20.0.28 (Enterprise Edition)
Android SDK: /Users/xamarin77/Desktop/android-sdk-macosx_êèéæâàçéè€ÿëïœ
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		3.2    (API level 13)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

=== Xamarin.Mac ===

Version: 1.10.0.18 (Enterprise Edition)

=== Build Information ===

Release ID: 505040015
Git revision: f93940a35458a18052f1a25e106e62ca970d9c40
Build date: 2014-11-19 15:32:41-05
Xamarin addins: dc23cbd91a3a0e1d326328e1229e86c942a49ec8

=== Operating System ===

Mac OS X 10.9.5
Darwin Xamarin77s-Mac-mini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-01-06 10:32:19 UTC
Fixed. It was simply a binding mistake.

xamcore/master: d47fc607b50cbc80756019e091d23e23e2f41b11

The following code can be used as a workaround until the fix is released:

var p = Runtime.GetNSObject (Messaging.IntPtr_objc_msgSend (arrayController.Handle, Selector.GetHandle ("newObject")));