Bug 1524 - set a StructLayout attribute to use Class with external code
Summary: set a StructLayout attribute to use Class with external code
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: Master
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: 2.0.x
Assignee: Chris Hamons
Depends on:
Reported: 2011-10-16 20:34 UTC by Vittorio G.
Modified: 2015-02-24 12:16 UTC (History)
3 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 Vittorio G. 2011-10-16 20:34:51 UTC
Hello, I am trying to implement MethodSwizzling as described here: http://lapcatsoftware.com/blog/2007/11/25/working-without-a-nib-part-6-working-without-a-xib/
It's great to see that all the objc runtime type are already present in monomac, however, after porting the sample code to c# i get an exception:

System.Runtime.InteropServices.MarshalDirectiveException has been thrown
Type MonoMac.ObjCRuntime.Class which is passed to unmanaged code must have a StructLayout attribute.

Here is the code that i wrote that triggers the exception:

        public static extern System.Reflection.MethodBase class_getClassMethod(Class aClass, Selector aSelector);
        public static extern void method_exchangeImplementations(System.Reflection.MethodBase a, System.Reflection.MethodBase b);
        private static void Main (string[] args)
            Class bundleClass = new Class("NSBundle");
            System.Reflection.MethodBase originalMethod = class_getClassMethod(bundleClass, new Selector("loadNibNamed:owner:"));
            System.Reflection.MethodBase categoryMethod = class_getClassMethod(bundleClass, new Selector("fake_loadNibNamed:owner:"));
            method_exchangeImplementations(originalMethod, categoryMethod);

The exception is triggere from the first class_getClassMethod call.
Comment 1 Zoltan Varga 2011-10-21 04:58:37 UTC
-> monomac.
Comment 2 Chris Hamons 2015-02-24 12:16:59 UTC
I'd like to apologize on the incredibly long delay in replying to this bug. We have a backlog of Xamarin.Mac bugs that we are working through and are working to prevent it from happening it again

So the issue is that you need to be using IntPtr (or some wrapper holding one), when you call objective-c. This works for me

		public static extern IntPtr class_getClassMethod(IntPtr aClass, Selector aSelector);
		public static extern void method_exchangeImplementations(IntPtr a, IntPtr b);

		public override void DidFinishLaunching (NSNotification notification)
			mainWindowController = new MainWindowController ();
			mainWindowController.Window.MakeKeyAndOrderFront (this);
			Class bundleClass = new Class("NSBundle");
			IntPtr originalMethod = class_getClassMethod(bundleClass.Handle, new Selector("loadNibNamed:owner:"));
			IntPtr categoryMethod = class_getClassMethod(bundleClass.Handle, new Selector("fake_loadNibNamed:owner:"));
			method_exchangeImplementations(originalMethod, categoryMethod);