cary
cary

Reputation: 41

Problems with generation of dynamic code

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

    }

Upvotes: 0

Views: 636

Answers (1)

Hans Passant
Hans Passant

Reputation: 942308

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.

Upvotes: 3

Related Questions