I’ve been working on a compiler for a custom language for .NET, its currently using System.Reflection.Emit but when I want to run it on Mono (It runs fine on MS .NET, both 2.0 and 4.0 CLRs) I run into a bunch of exceptions when dealing with GenericTypeParameterBuilder, most specifically this one:
Unhandled Exception: System.NotSupportedException: The invoked member is not supported in a dynamic module.
at System.Reflection.Emit.TypeBuilder.check_created () [0x00012] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.10.9\mcs\class\corlib\System.Reflection.Emit\TypeBuilder.cs:1678
at System.Reflection.Emit.TypeBuilder.InternalResolve () [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.10.9\mcs\class\corlib\System.Reflection.Emit\TypeBuilder.cs:1653
at System.Reflection.Emit.GenericTypeParameterBuilder.InternalResolve () [0x00000] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.10.9\mcs\class\corlib\System.Reflection.Emit\GenericTypeParameterBuilder.cs:93
at System.Reflection.MonoGenericClass.InternalResolve () [0x00021] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.10.9\mcs\class\corlib\System.Reflection\MonoGenericClass.cs:105
at System.Reflection.Emit.TypeBuilder.DefineDefaultConstructor (MethodAttributes attributes) [0x00030] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.10.9\mcs\class\corlib\System.Reflection.Emit\TypeBuilder.cs:484
at System.Reflection.Emit.TypeBuilder.CreateType () [0x0017f] in C:\cygwin\tmp\monobuild\build\BUILD\mono-2.10.9\mcs\class\corlib\System.Reflection.Emit\TypeBuilder.cs:788
at dotC.CType.CreateType () [0x00000] in <filename unknown>:0
at dotC.Compiler.Save () [0x00000] in <filename unknown>:0
at dotC.Compiler.Compile () [0x00000] in <filename unknown>:0
at dotC.Dev.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0
Now the code that runs this is pretty involved so it is hard to copy-paste a specific piece of code, but the code structure I am trying to compile looks like this:
public class Foo<T> { }
public class Bar<T> : Foo<T> { }
And the problem happens when I am trying to compile the Bar<T> class which is supposed to inherit from Foo<T> where the generic parameter supplied to Foo is the T from bar.
So my question is: Is this a known issue in Mono SRE? Is there a way to work around this? Is it fixed in a new beta of mono or something, etc.?
This certainly looks like a bug. It appears that explicitly calling
DefineDefaultConstructoron the child type’s type builder prior to setting the parent type and callingCreateTypewill work around it, at least in the simplest case.EDIT
Alternatively, explicitly defining a constructor for the child type (which potentially just calls the base constructor and returns) looks like it works even after setting the parent type, so that may be a better approach.