小编典典

如何获得方法参数的名称?

c#

如果我有如下方法:

public void MyMethod(int arg1, string arg2)

我将如何获取参数的实际名称?我似乎在MethodInfo中找不到任何东西,这实际上会给我参数的名称。

我想写一个看起来像这样的方法:

public static string GetParamName(MethodInfo method, int index)

因此,如果我通过以下方式调用此方法:

string name = GetParamName(MyMethod, 0)

它会返回“ arg1”。这可能吗?


阅读 437

收藏
2020-05-19

共1个答案

小编典典

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;
}

以上示例应满足您的需求。

2020-05-19