Bug 13771 - InvalidCastException when accessing RepresentedObject in NSTreeView
Summary: InvalidCastException when accessing RepresentedObject in NSTreeView
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2013-08-06 01:12 UTC by David Lechner
Modified: 2014-02-21 11:23 UTC (History)
4 users (show)

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

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:

Description David Lechner 2013-08-06 01:12:14 UTC
I am trying to use bindings to bind an NSTreeController to a NSOutlineView. I have run across a problem. When I try to get the RepresentedObject, I get an InvalidCastException. I don't see a way to work around this, so I am stuck.

Here is is some sample code that causes the exception:

var treeNode = NSTreeNode.FromRepresentedObject (new NSObject ());
var representedItem = treeNode.RepresentedObject;

I'd be glad to dig deeper, but I am not finding the source code for NSTreeNode on github.

Let me know what else I can do to help.

Here is what I am running. Not exactly sure how to tell what version of MonoMac I am running though.

=== Xamarin Studio ===

Version 4.0.11 (build 6)
Installation UUID: 7de50ea0-5da0-4937-9d47-d071aa496e85
	Mono 3.2.1 ((no/f3f789e)
	GTK 2.24.20
	GTK# (
	Package version: 302010001

=== Apple Developer Tools ===

Xcode 4.6.3 (2068)
Build 4H1503

=== Xamarin.iOS ===

Not Installed

=== Xamarin.Android ===

Not Installed

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Build Information ===

Release ID: 400110006
Git revision: b8a862fc80adef69bb383dd305619186da9e1f9b
Build date: 2013-07-31 09:26:05+0000
Xamarin addins: e0bfb020a2d4ed62aadd4d6f40bd83dcbac98ae3

=== Operating System ===

Mac OS X 10.8.4
Darwin freyja.lechnology.com 12.4.0 Darwin Kernel Version 12.4.0
    Wed May  1 17:57:12 PDT 2013
    root:xnu-2050.24.15~1/RELEASE_X86_64 x86_64
Comment 1 David Lechner 2013-08-06 01:25:28 UTC
Here is the stack trace.

Unhandled Exception:
System.InvalidCastException: Cannot cast from source type to destination type.
at MonoMac.AppKit.NSTreeNode.get_RepresentedObject () <IL 0x00027, 0x000b9>
at KeePass2Mac.GroupOutlineViewDelegate.GetView (MonoMac.AppKit.NSOutlineView,MonoMac.AppKit.NSTableColumn,MonoMac.Foundation.NSObject) [0x0000d] in /Users/david/src/KeePass2Mac/KeePass2Mac/GroupOutlineViewDelegate.cs:44
at (wrapper dynamic-method) object.[KeePass2Mac.GroupOutlineViewDelegate.MonoMac.AppKit.NSView GetView(MonoMac.AppKit.NSOutlineView, MonoMac.AppKit.NSTableColumn, MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.AppKit.NSOutlineView,MonoMac.AppKit.NSTableColumn,MonoMac.Foundation.NSObject) <IL 0x0001d, 0x00083>
at (wrapper native-to-managed) object.[KeePass2Mac.GroupOutlineViewDelegate.MonoMac.AppKit.NSView GetView(MonoMac.AppKit.NSOutlineView, MonoMac.AppKit.NSTableColumn, MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.AppKit.NSOutlineView,MonoMac.AppKit.NSTableColumn,MonoMac.Foundation.NSObject) <IL 0x000ab, 0x00267>
at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00003>
at MonoMac.AppKit.NSApplication.Main (string[]) <IL 0x00041, 0x00103>
at KeePass2Mac.MainClass.Main (string[]) [0x00007] in /Users/david/src/KeePass2Mac/KeePass2Mac/Main.cs:35
Comment 2 Richard Markiewicz 2013-09-11 09:44:33 UTC
The problem is, the return type of representedObject is incorrectly specified as NSTreeNode, rather than NSObject.

I made a pull request to fix that here: https://github.com/mono/monomac/pull/116

If you are using monomac, you can merge that fix and just recompile.

If you are using Xamarin.Mac, you will have to wait for this to be merged and released. A workaround in the meantime could be to get the right behaviour using an extension method, e.g.

	public static class Ext
		static readonly IntPtr selRepresentedObjectHandle = Selector.GetHandle("representedObject");

		public static NSObject GetRepresentedObject(this NSTreeNode treeNode)
			global::MonoMac.AppKit.NSApplication.EnsureUIThread ();
			NSObject ret;

			ret = Runtime.GetNSObject(MonoMac.ObjCRuntime.Messaging.IntPtr_objc_msgSend(treeNode.Handle, selRepresentedObjectHandle));

			return ret;

        var representedItem = treeNode.GetRepresentedObject();
Comment 3 Miguel de Icaza [MSFT] 2014-02-11 23:31:45 UTC
Thanks, fixed!
Comment 4 Tim Uy 2014-02-21 09:40:41 UTC
Is this fixed in XamMac? I'm still seeing the issue.
Comment 5 Miguel de Icaza [MSFT] 2014-02-21 11:23:38 UTC
It is part of the upcoming release.