Bug 1082 - Delegate.CreateDelegate does not treat the "this" parameter in structs as by-ref.
Summary: Delegate.CreateDelegate does not treat the "this" parameter in structs as by-...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 2.10.x
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-09-27 11:47 UTC by Michael Burbea
Modified: 2012-01-20 19:45 UTC (History)
2 users (show)

Tags:
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 GitHub or Developer Community 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 Michael Burbea 2011-09-27 11:47:28 UTC
It appears that mono when using Delegate.CreateDelegate to create an open instance delegate for a struct method does not treat the "this" parameter like a ref parameter. On the CLR, the "this" parameter is treated like a ref parameter but in mono it is treated like a normal parameter. I believe this behavior is incorrect as struct instance methods are normally invoked as by-reference calls.

The following code demonstrates the problem::


		public struct Foo
		{
			public void SomeMethod()
			{
			}
		}
		private delegate void FooDelegate(ref Foo foo);

		static void Main()
		{
			Delegate @delegate;
			MethodInfo method = typeof(Foo).GetMethod("SomeMethod");
			try
			{
				@delegate = Delegate.CreateDelegate(typeof(FooDelegate), method);
			}
			catch (Exception e)
			{
				Console.WriteLine("This fails in mono");
				Console.WriteLine(e);
			}
			try
			{
				@delegate = Delegate.CreateDelegate(typeof(Action<Foo>), method);
			}
			catch (Exception e)
			{
				Console.WriteLine("This fails in CLR");
				Console.WriteLine(e);
			}
		}
	}
Comment 1 Zoltan Varga 2012-01-20 19:45:02 UTC
This has already been fixed in HEAD by cd376b7a96347a9d94764fb3ac6e6aacf84d7917.