mercredi 30 décembre 2015

System.Reflection.Emit - If Statement Generation

Good evening!

I've been having some issues with my program for the last few days, I'm trying to learn how If statements are generated with ILGenerator in C#.

Here's my code: (ilg is an ILGenerator)

    ilg.Emit(OpCodes.Ldc_I4_1);
    Label start = ilg.DefineLabel();
    ilg.Emit(OpCodes.Brfalse, start);
    ilg.Emit(OpCodes.Ldstr, "Hello");
    ilg.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }));
    ilg.MarkLabel(start);

Some notes:

  • Alone, calling Console.WriteLine works perfectly.
  • This is the error that I get when I run the generated exe:

    "Unhandled Exception: System.InvalidProgramException: Common Language Runtime detected an invalid program." at Testing.Test.Main(String[] )
    
    

Thanks!





Aucun commentaire:

Enregistrer un commentaire