Joh*_*anP 1 c# reflection.emit
我正在尝试DynamicMethod并尝试使用它IL来创建一些对象。我想创建以下非常基本的对象:
new Queue<double>(new List<double>{100});
Run Code Online (Sandbox Code Playgroud)
我已经使用 ILDASM 来查看OpCodes生成此文件需要什么。这是 ILDASM 告诉我的:
IL_0000: newobj instance void class [System.Collections]System.Collections.Generic.List`1<float64>::.ctor()
IL_0005: dup
IL_0006: ldc.r8 100.
IL_000f: callvirt instance void class [System.Collections]System.Collections.Generic.List`1<float64>::Add(!0)
IL_0014: newobj instance void class [System.Collections]System.Collections.Generic.Queue`1<float64>::.ctor(class [System.Runtime]System.Collections.Generic.IEnumerable`1<!0>)
IL_0019: pop
IL_001a: ret
Run Code Online (Sandbox Code Playgroud)
这就是我正在做的:
var dynMethod = new DynamicMethod("QueueMaker", typeof(Queue<double>), Type.EmptyTypes);
ILGenerator ilGen = dynMethod.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, typeof(List<double>).GetConstructor(Type.EmptyTypes));
ilGen.Emit(OpCodes.Dup);
ilGen.Emit(OpCodes.Ldc_R8, 100);
ilGen.EmitCall(OpCodes.Callvirt, typeof(List<double>).GetMethod("Add"), null);
ilGen.Emit(OpCodes.Newobj, typeof(Queue<double>).GetConstructor(new[] { typeof(IEnumerable<double>) }));
ilGen.Emit(OpCodes.Pop);
ilGen.Emit(OpCodes.Ret);
var returnFunc = (Func<Queue<double>>)dynMethod.CreateDelegate(typeof(Func<Queue<double>>));
var queue = returnFunc();
Run Code Online (Sandbox Code Playgroud)
我得到异常System.InvalidProgramException: 'Common Language Runtime detected an invalid program.'我做错了什么?
我究竟做错了什么?
两件事情:
(1)
ilGen.Emit(OpCodes.Ldc_R8, 100);
Run Code Online (Sandbox Code Playgroud)
这是传递不正确的值类型。确保调用以下重载double:
ilGen.Emit(OpCodes.Ldc_R8, (double)100); // or 100d
Run Code Online (Sandbox Code Playgroud)
(2)
ilGen.Emit(OpCodes.Pop);
Run Code Online (Sandbox Code Playgroud)
最有可能的是 ILDASM 包含了这一点,因为在这里new Queue<double>(new List<double>{100});您将丢弃结果,但是当您需要将结果返回给调用者时,该指令无效。Dup将新List<double>实例保存在计算堆栈上的指令已被构造函数调用消耗,Queue<double>因此这会从堆栈中删除结果,最终导致堆栈无效。
删除该行,问题就解决了。