Bug 34016 - InvalidOperationException should be thrown when calling getter of C# delegate event properties if delegate class property is already set
Summary: InvalidOperationException should be thrown when calling getter of C# delegate...
Status: CONFIRMED
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Other ()
Version: 2.0.2
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-09-16 15:15 UTC by Sandy Armstrong [MSFT]
Modified: 2016-09-26 21:59 UTC (History)
2 users (show)

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


Attachments
Simple test case (12.12 KB, application/zip)
2015-09-16 15:15 UTC, Sandy Armstrong [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 for Bug 34016 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Sandy Armstrong [MSFT] 2015-09-16 15:15:27 UTC
Created attachment 12917 [details]
Simple test case

In Xamarin.Mac, when handling view events, you are not supposed to mix using delegate classes with using C# delegate events.  An InvalidOperationException helps to prevent this. For example, if I do the following, I will get an exception on the last line:

			var tv = new NSTableView ();
			tv.Delegate = new NSTableViewDelegate();
			var cgrv = tv.CoreGetRowView;

This is good. It prevents the NSTableView.Delegate we set from getting messed with.

However, the exception fails to get thrown if I am working with the subclass NSOutlineView:

			var ov = new NSOutlineView ();
			ov.Delegate = new NSOutlineViewDelegate();
			var cgrv = ov.CoreGetRowView;
			// Now ov.Delegate shows as null

Accessing the getter of the C# delegate event property causes the underlying delegate class to be replaced. It's especially confusing because it's replaced with an NSTableViewDelegate, and therefore can only be accessed by downcasting the NSOutlineView to an NSTableView. NSOutlineView.Delegate returns null.

We came across this while working on the inspector, which inspects property values for selected view. View event handling would break simply by accessing the getters on these C# delegate event properties.

Test case attached.