小编Dan*_*vic的帖子

System.Reflection.Emit - 如果语句生成

我正在尝试学习如何If在C#中使用ILGenerator生成语句.

这是我的代码:(ilg是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);
Run Code Online (Sandbox Code Playgroud)

一些说明:

  • 独自调用Console.WriteLine完美运行.
  • 这是我运行生成的exe时得到的错误:

未处理的异常:System.InvalidProgramException:公共语言运行时检测到无效的程序.在Testing.Test.Main(String [])

c# compiler-construction reflection clr il

5
推荐指数
1
解决办法
1117
查看次数

标签 统计

c# ×1

clr ×1

compiler-construction ×1

il ×1

reflection ×1