生成动态代码的问题

发布于 2024-09-02 01:25:59 字数 2164 浏览 3 评论 0原文

此代码 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2024-09-09 01:25:59

您的 IL 根本无效。例外情况并不好,但您正在 JIT 编译器的内部进行修改。 Opcodes.Ldsftn 确实做您认为它做的事情,您需要 Ldsfld 来加载字段。我真的无法弄清楚你要编写什么代码,但这应该很接近。无论如何它都不会崩溃:

        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.Ldsfld, campoBld);
        il.Emit(OpCodes.Ldsfld, campoBld1);
        il.Emit(OpCodes.Add);
        il.Emit(OpCodes.Pop);
        il.Emit(OpCodes.Ret);

弄清楚要使用什么 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:

        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.Ldsfld, campoBld);
        il.Emit(OpCodes.Ldsfld, campoBld1);
        il.Emit(OpCodes.Add);
        il.Emit(OpCodes.Pop);
        il.Emit(OpCodes.Ret);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文