首页 > 解决方案 > IlGenerator 发射

问题描述

我正在玩弄DynamicMethod并尝试使用它IL来创建一些对象。我想创建以下非常基本的对象:

new Queue<double>(new List<double>{100});

我已经使用 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

这就是我正在做的事情:

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();

我得到异常System.InvalidProgramException: 'Common Language Runtime detected an invalid program.'我做错了什么?

标签: c#reflection.emit

解决方案


我究竟做错了什么?

两件事情:

(1)

ilGen.Emit(OpCodes.Ldc_R8, 100);

这是传递不正确的值类型。确保调用重载double

ilGen.Emit(OpCodes.Ldc_R8, (double)100); // or 100d

(2)

ilGen.Emit(OpCodes.Pop);

很可能 ILDASM 包含了这一点,因为在这里new Queue<double>(new List<double>{100});您要丢弃结果,但是当您需要将结果返回给调用者时,此指令无效。Dup将新实例保存在评估堆栈上的指令List<double>已被Queue<double>构造函数调用消耗,因此这会从堆栈中删除结果,这最终会导致堆栈无效。

删除该行,问题将得到解决。


推荐阅读