Note that there are some explanatory texts on larger screens.

plurals
  1. POReflection.Emit and generic types
    text
    copied!<p>I am using <code>Reflection.Emit</code> and I want to create a type that would be the equivalent of the following type defined in C#:</p> <pre><code>class A { public Tuple&lt;A, int&gt; GetValue(int x) { return new Tuple&lt;A, int&gt;(this, x); } } </code></pre> <p>The trick is that I need to use a generic type from BCL that uses my custom type as a generic argument.</p> <p>I'm messing with the following snippet:</p> <pre><code>var asmName = new AssemblyName("Test"); var access = AssemblyBuilderAccess.Run; var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, access); var module = asm.DefineDynamicModule("Test"); var aType = module.DefineType("A"); var tupleType = typeof(Tuple&lt;,&gt;).MakeGenericType(aType, typeof(int)); var attrs = MethodAttributes.Public; var method = aType.DefineMethod("GetValue", attrs, tupleType, new [] { typeof(int) }); var gen = method.GetILGenerator(); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Ldarg_1); // here is the fail: var ctor = tupleType.GetConstructor(new [] { typeof(int), aType } ); gen.Emit(OpCodes.Newobj, ctor); </code></pre> <p>The call to <code>GetConstructor</code> fails with the following exception:</p> <blockquote> <p>NotSupportedException: Specified method is not supported.</p> </blockquote> <p>So, basically, it won't let me get the constructor of a type that merely references my custom type, and neither can I finalize the type before emitting the body of its method.</p> <p>Can it really be impossible to get out of this vicious circle?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload