首页 / C# / C#-在方法参数上设置名称
C#-在方法参数上设置名称
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C#-在方法参数上设置名称,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1801字,纯文字阅读大概需要3分钟。
内容图文
我正在使用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文档是否有这样做的线索,因为这是拥有此类信息但无济于事的逻辑位置.
有人有建议吗?
解决方法:
看起来MethodBuilder.DefineParameter允许您指定参数名称:
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.
内容总结
以上是互联网集市为您收集整理的C#-在方法参数上设置名称全部内容,希望文章能够帮你解决C#-在方法参数上设置名称所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。