在方法参数上设置名称(set name on method arguments)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
在方法参数上设置名称(set name on method arguments)

我正在使用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::Main

contrast 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::Main

the 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.

更多推荐

本文发布于:2023-04-17 09:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/50804ee4cbcf415ce52b13a48a706ef6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:名称   参数   方法   arguments   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!