你怎么能得到的方法参数的名字呢? [英] How can you get the names of method parameters?

查看:312
本文介绍了你怎么能得到的方法参数的名字呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个方法,如:

 公共无效的MyMethod(INT ARG1,串ARG2)
 

我将如何去获得的参数的实际名称? 我似乎无法找到任何的MethodInfo的,这将真正给我的参数的名称。

我想编写一个方法,该方法是这样的:

 公共静态字符串GetParamName(MethodInfo的方法,INT指数)
 

所以,如果我称这种方法:

 字符串名称= GetParamName(的MyMethod,0)
 

这将返回ARG1。这可能吗?

解决方案

 公共静态字符串GetParamName(System.Reflection.MethodInfo方法,INT指数)
{
    字符串retVal的=的String.Empty;

    如果(方法= NULL和放大器;!&安培; method.GetParameters()长度和GT;指数)
        retVal的= method.GetParameters()[指数] .Name点;


    返回retVal的;
}
 

在上面的示例应该做你所需要的。

If I have a method such as:

public void MyMethod(int arg1, string arg2)

How would I go about getting the actual names of the arguments? I can't seem to find anything in the MethodInfo which will actually give me the name of the parameter.

I would like to write a method which looks like this:

public static string GetParamName(MethodInfo method, int index)

So if I called this method with:

string name = GetParamName(MyMethod, 0)

it would return "arg1". Is this possible?

解决方案

public static string GetParamName(System.Reflection.MethodInfo method, int index)
{
    string retVal = string.Empty;

    if (method != null && method.GetParameters().Length > index)
        retVal = method.GetParameters()[index].Name;


    return retVal;
}

The above sample should do what you need.

这篇关于你怎么能得到的方法参数的名字呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆