我正在使用Reflection.Emit来创建一个exe。 到目前为止,我已经可以创建一个可用的CIL PE了。 (它只是将一个字符串输出到Console.WriteLine。)但是main方法的参数是自动生成的(A_0)。
.method public static void Main(string[] A_0) cil managed { .entrypoint // Code size 12 (0xc) .maxstack 1 IL_0000: nop IL_0001: ldstr "Cafe con pan" IL_0006: call void [mscorlib]System.Console::WriteLine(string) IL_000b: ret } // end of method Program::Main将其与相应C#程序的代码进行对比
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "Cafe con pan" IL_0006: call void [mscorlib]System.Console::WriteLine(string) IL_000b: nop IL_000c: ret } // end of method Program::Main参数名称是args。 我怎样才能为这个论点命名? 我用来制作方法的代码如下所示:
Il = System.reflection.Emit Re = System.Reflection tb = Reflection.Emit.TypeBuilder Il.MethodBuilder meth = tb.DefineMethod( "Main", // name Re.MethodAttributes.Public | Re.MethodAttributes.Static, // method attributes typeof(void), // return type new Type[] { typeof(String[]) }); // parameter types Il.ILGenerator methIL = meth.GetILGenerator(); methIL.Emit(Il.OpCodes.Nop); methIL.Emit(Il.OpCodes.Ldstr, "Cafe con pan"); Type [] args = new Type []{typeof(string)}; Re.MethodInfo printString = typeof(Console).GetMethod("WriteLine", args); methIL.Emit(Il.OpCodes.Call, printString); methIL.Emit(Il.OpCodes.Ret);我已经检查了TypeBuilder.DefineMethod文档以获取任何线索,因为它是获得此类信息但无济于事的合理位置。 有没有人有建议?
I am using Reflection.Emit to make a exe. I have come so far that I can create a working CIL PE. (It just output a string to Console.WriteLine.) But the argument to main method is automaticly generated (A_0).
.method public static void Main(string[] A_0) cil managed { .entrypoint // Code size 12 (0xc) .maxstack 1 IL_0000: nop IL_0001: ldstr "Cafe con pan" IL_0006: call void [mscorlib]System.Console::WriteLine(string) IL_000b: ret } // end of method Program::Maincontrast it with the code from a corresponding C# program
.method private hidebysig static void Main(string[] args) cil managed { .entrypoint // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldstr "Cafe con pan" IL_0006: call void [mscorlib]System.Console::WriteLine(string) IL_000b: nop IL_000c: ret } // end of method Program::Mainthe argument name is args. How can I give name to the argument? The code I use for making the method look like this:
Il = System.reflection.Emit Re = System.Reflection tb = Reflection.Emit.TypeBuilder Il.MethodBuilder meth = tb.DefineMethod( "Main", // name Re.MethodAttributes.Public | Re.MethodAttributes.Static, // method attributes typeof(void), // return type new Type[] { typeof(String[]) }); // parameter types Il.ILGenerator methIL = meth.GetILGenerator(); methIL.Emit(Il.OpCodes.Nop); methIL.Emit(Il.OpCodes.Ldstr, "Cafe con pan"); Type [] args = new Type []{typeof(string)}; Re.MethodInfo printString = typeof(Console).GetMethod("WriteLine", args); methIL.Emit(Il.OpCodes.Call, printString); methIL.Emit(Il.OpCodes.Ret);I have checked the TypeBuilder.DefineMethod documentation for any clue to do that as it is the logical place to have such an info but with no avail. Do anyone have a suggestion?
最满意答案
看起来像MethodBuilder.DefineParameter允许您指定参数名称:
设置此方法的参数属性和参数名称 ,或此方法的返回值。 返回可用于应用自定义属性的ParameterBuilder。
Looks like MethodBuilder.DefineParameter allows you to specify parameter names:
Sets the parameter attributes and the name of a parameter of this method, or of the return value of this method. Returns a ParameterBuilder that can be used to apply custom attributes.
更多推荐
发布评论