- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 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.'
我做错了什么?
最佳答案
What am I doing wrong?
两件事:
(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>
使用构造函数调用,因此这会从堆栈中删除结果,这最终会导致堆栈无效。
去掉那一行,问题就解决了。
关于c# - IlGenerator 发射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53716931/
给出以下代码: using System; using System.Reflection.Emit; using System.Diagnostics; using System.Reflectio
我正在尝试构造一个泛型类型的实例并在该实例上调用一个方法。然后返回方法的结果。 var genericType = typeof(GenericType<>).MakeGenericType(type
我想得到这个: .property instance class [WorldTool.Core]WorldTool.IInputPort SomePort { .get instance c
我正在尝试使用 DynamicMethod 并尝试使用 IL 来创建一些对象。我想创建以下非常基本的对象: new Queue(new List{100}); 我已经使用 ILDASM 查看生成此代码
我正在使用 System.Reflection.Emit 为类型生成包装器。在某一时刻,原始对象可能会在访问时抛出错误 (FaultException),并且该错误应该被我的 try { } catc
Food obj = ...; ILGenerator gen = (...).GetILGenerator(); gen.Emit( ?? obj ?? ); // replace this gen
我使用 ILGenerator 生成一些 IL,这是我的代码: DynamicMethod method = new DynamicMethod("test", null, Type.EmptyTyp
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我必须将一个对象的实例传递给一个函数,所以显然所有要作为参数的信息都将加载到评估堆栈中这是我正在寻找的代码 someClass SomeObject = new someClass(); il.Emi
使用 Ildasm 我得到了这个: .method public hidebysig virtual instance string Mymethod() cil manage
我已经编写了一些函数来使用 ILGenerator 创建一个 exe 文件。我想要的是向用户展示在不使用外部工具(如 ILDasm 或 Reflector)的情况下生成的 IL 语言。 在我的程序执行
考虑为我自己的语言制作编译器,我尝试使用 Reflection.Emit 框架生成一些 MSIL 代码。当我声明局部变量时使用 int 时它工作正常。但是,当我想声明一个尚未编译的类型的局部变量时,我
我正在使用 ILGenerator.Emit 生成动态类型。我正在生成一个方法主体,它将方法参数的类型存储在一个数组中。为了实际将元素存储在数组中,我循环遍历给定方法的参数并构建必要的 IL 来存储元
如何使用 ILGenerator 设置 .maxstack 指令? 例如,典型的 setter 方法的 .maxstack 为 2: .maxstack 2 // The evalua
我如何使用 ILGenerator.Emit(不是 LambdaExpression.Compile?因为我正在尝试寻找性能更高的解决方案)动态创建具有未知 MethodInfo 的委托(delega
我有一些代码使用 IlGenerator.Emit 使用数据读取器创建和填充通用对象。它工作得很好,但是我需要扩展它以在数据库字段名称包含下划线时填充简单的子对象。 例如,名为“Address_Lin
基本上,我试图将字节数组中的数据反序列化为对象。我正在尝试使用 UTF8 编码的 GetString 方法来读取字符串。这是我的部分代码: var mm = new DynamicMethod("ge
我正在使用 System.Reflection.Emit,有时我想从 MethodBuilder 创建一个委托(delegate): MethodBuilder fooBuilder = create
我正在尝试通过在运行时发出 IL 来编译 DynamicMethod。我希望它执行以下操作: array.OrderByDesc( /* Select Field/Property Expressio
我添加到 ILGenerator 字符串 ilGen.Emit(OpCodes.Ldarg_0); ilGen.Emit(OpCodes.Ldfld, readField); 和 ilGen.Emit
我是一名优秀的程序员,十分优秀!