Note that there are some explanatory texts on larger screens.

plurals
  1. POEmitting Generic Method using Reflection Throws an Exception at Invoke
    primarykey
    data
    text
    <p>I am trying this simple example on creating a generic method using Reflection.Emit but it is causing an exception when calling Invoke and I can't find the problem.</p> <pre><code>public class Program { public static void Main(string[] args) { AppDomain appDomain = AppDomain.CurrentDomain; AssemblyName name = new AssemblyName("MyAssembly") { Version = new Version("1.0.0.0") }; AssemblyBuilder ab = appDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.RunAndSave); ModuleBuilder mb = ab.DefineDynamicModule("MyModule", "MyAssembly.dll"); TypeBuilder tb = mb.DefineType("Widget", TypeAttributes.Public); // Define a method builder MethodBuilder methodBuilder = tb.DefineMethod("MyGenericMethod", MethodAttributes.Public | MethodAttributes.Static); // Get generic type parameter builders GenericTypeParameterBuilder[] genericParams = methodBuilder.DefineGenericParameters("TKey", "TValue"); methodBuilder.SetSignature(typeof(int), null, null, genericParams, null, null); methodBuilder.DefineParameter(1, ParameterAttributes.None, "key"); methodBuilder.DefineParameter(2, ParameterAttributes.None, "val"); ILGenerator gen = methodBuilder.GetILGenerator(); MethodInfo writeLineStr = typeof(Console).GetMethod("WriteLine", new[] { typeof(object) }); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Box, genericParams[0]); gen.Emit(OpCodes.Call, writeLineStr); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Box, genericParams[1]); gen.Emit(OpCodes.Call, writeLineStr); gen.Emit(OpCodes.Ret); Type t = tb.CreateType(); MethodInfo genMeth = t.GetMethod("MyGenericMethod").MakeGenericMethod(typeof(string), typeof(int)); genMeth.Invoke(null, new object[] { "NumberKey", 100 }); } } </code></pre> <p>When calling genMeth.Invoke, the following exception is thrown</p> <pre><code>Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the t arget of an invocation. ---&gt; System.InvalidProgramException: Common Language Runtime detected an inv alid program. at Widget.MyGenericMethod[TKey,TValue](TKey key, TValue val) --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Bool ean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Obje ct[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at ReflectionEmitDemo14.Program.Main(String[] args) in d:\Projects\ReflectionEmitDemo\ReflectionE mitDemo14\Program.cs:line 50 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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