生成动态代码的问题
此代码 gif 出现异常:调用异常,请帮忙,我不知道发生了什么,我认为与 Add 有关,因为当我推入堆栈整数时他起作用,而当我推入左值时它不起作用,谢谢
static void Main(string[] args)
{
AppDomain dominioAplicacion = System.Threading.Thread.GetDomain();
AssemblyName nombre_Del_Ensamblado = new AssemblyName("ASS");
AssemblyBuilder ensambladoBld = dominioAplicacion.DefineDynamicAssembly(nombre_Del_Ensamblado, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder moduloBld = ensambladoBld.DefineDynamicModule("<MOD");
TypeBuilder claseContenedoraBld = moduloBld.DefineType("claseContenedora");
MethodBuilder mainBld = claseContenedoraBld.DefineMethod("main", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
ILGenerator il = mainBld.GetILGenerator();
FieldBuilder campoBld = claseContenedoraBld.DefineField("x", typeof(int), FieldAttributes.Public | FieldAttributes.Static);
il.Emit(OpCodes.Ldc_I4, 2);
il.Emit(OpCodes.Stsfld, campoBld);
FieldBuilder campoBld1 = claseContenedoraBld.DefineField("x1", typeof(int), FieldAttributes.Public | FieldAttributes.Static);
il.Emit(OpCodes.Ldc_I4, 2);
il.Emit(OpCodes.Stsfld, campoBld1);
il.Emit(OpCodes.Ldftn, campoBld);
//il.Emit(OpCodes.Unbox, typeof(int));
//il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldftn, campoBld1);
//il.Emit(OpCodes.Unbox, typeof(int));
il.Emit(OpCodes.Stloc_1);
il.Emit(OpCodes.Ldloc_1);
//il.Emit(OpCodes.Box, typeof(int));
//il.Emit(OpCodes.Ldftn, campoBld1);
//il.Emit(OpCodes.Unbox, typeof(int));
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Pop);
//il.Emit(OpCodes.Stsfld, campoBld1);
il.Emit(OpCodes.Ret);
Type t = claseContenedoraBld.CreateType();
object ptInstance = Activator.CreateInstance(t, new Type[] { });
t.InvokeMember("main", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null,
ptInstance,
new object[0]);
var x = t.GetField("x");
}
This code gif an exception: Invocation exception, please help, I don't know what happen, I think is some thing with the Add because he work when I push onto the stack intergers, and when i push lvalue It's didn't work, thanks
static void Main(string[] args)
{
AppDomain dominioAplicacion = System.Threading.Thread.GetDomain();
AssemblyName nombre_Del_Ensamblado = new AssemblyName("ASS");
AssemblyBuilder ensambladoBld = dominioAplicacion.DefineDynamicAssembly(nombre_Del_Ensamblado, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder moduloBld = ensambladoBld.DefineDynamicModule("<MOD");
TypeBuilder claseContenedoraBld = moduloBld.DefineType("claseContenedora");
MethodBuilder mainBld = claseContenedoraBld.DefineMethod("main", MethodAttributes.Public | MethodAttributes.Static, typeof(void), Type.EmptyTypes);
ILGenerator il = mainBld.GetILGenerator();
FieldBuilder campoBld = claseContenedoraBld.DefineField("x", typeof(int), FieldAttributes.Public | FieldAttributes.Static);
il.Emit(OpCodes.Ldc_I4, 2);
il.Emit(OpCodes.Stsfld, campoBld);
FieldBuilder campoBld1 = claseContenedoraBld.DefineField("x1", typeof(int), FieldAttributes.Public | FieldAttributes.Static);
il.Emit(OpCodes.Ldc_I4, 2);
il.Emit(OpCodes.Stsfld, campoBld1);
il.Emit(OpCodes.Ldftn, campoBld);
//il.Emit(OpCodes.Unbox, typeof(int));
//il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ldftn, campoBld1);
//il.Emit(OpCodes.Unbox, typeof(int));
il.Emit(OpCodes.Stloc_1);
il.Emit(OpCodes.Ldloc_1);
//il.Emit(OpCodes.Box, typeof(int));
//il.Emit(OpCodes.Ldftn, campoBld1);
//il.Emit(OpCodes.Unbox, typeof(int));
il.Emit(OpCodes.Add);
il.Emit(OpCodes.Pop);
//il.Emit(OpCodes.Stsfld, campoBld1);
il.Emit(OpCodes.Ret);
Type t = claseContenedoraBld.CreateType();
object ptInstance = Activator.CreateInstance(t, new Type[] { });
t.InvokeMember("main", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
null,
ptInstance,
new object[0]);
var x = t.GetField("x");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 IL 根本无效。例外情况并不好,但您正在 JIT 编译器的内部进行修改。 Opcodes.Ldsftn 确实不做您认为它做的事情,您需要 Ldsfld 来加载字段。我真的无法弄清楚你要编写什么代码,但这应该很接近。无论如何它都不会崩溃:
弄清楚要使用什么 IL 的最佳方法是首先用 C# 编写代码,然后使用 Ildasm.exe 反汇编它以查看 IL 的样子。
Your IL is simply invalid. The exception isn't great but you're tinkering in the bowels of the JIT compiler. Opcodes.Ldsftn does not do what you think it does, you'll need Ldsfld to load a field. I can't really figure out what code you're trying to write but this ought to be close. It doesn't crash anyway:
The best way to figure out what IL to use is to write your code in C# first, then disassemble it with Ildasm.exe to see what the IL looks like.