Bug 40088

Summary: Call in lamda of static method with dynamic parameter fails in constructor argument
Product: [Mono] Compilers Reporter: Søren Bjergmark <sahb1239>
Component: C#Assignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: mono-bugs+mono, sahb1239
Priority: ---    
Version: unspecified   
Target Milestone: ---   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description Søren Bjergmark 2016-04-04 21:36:31 UTC
When calling a static method with a dynamic parameter in a lamda as a argument for the base class constructor the compiler gives this error on Mac OS X:
Error CS1673: Anonymous methods inside structs cannot access instance members of `this'. Consider copying `this' to a local variable outside the anonymous method and using the local instead (CS1673) (POCBugCompiler)

The following code works in Xamarin Studio and Visual Studio on Windows however not on Mac OS X. I am using version 5.10.3 (build 51 of Xamarin Studio) - no updates on stable channel. Mono is version 4.2.3.

using System;

namespace POCCompilerError
{
	public class MyClass
	{
		public class SomeBaseClass
		{
			public SomeBaseClass(Action action)
			{

			}
		}

		public class SomeInheritedClass : SomeBaseClass
		{
			public SomeInheritedClass() : base(() =>
				{
					StaticMethod(SomeRandomDynamic());
				})
			{

			}

			public static decimal StaticMethod(dynamic someDynamicArgument)
			{
				return 3m;
			}

			public static dynamic SomeRandomDynamic()
			{
				return new object();
			}
		}
	}
}

I discovered the bug in a similar call but the static method was located in the base class. If needed i would help with providing more information.
Comment 1 Marek Safar 2016-04-11 18:41:02 UTC
Fixed in master and mono 4.4