本文介绍了如何获取方法参数的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如果我有一个方法,例如:

If I have a method such as:

public void MyMethod(int arg1, string arg2)

我将如何获取参数的实际名称?我似乎无法在 MethodInfo 中找到任何实际上会给我参数名称的内容.

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)

它会返回arg1".这可能吗?

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.

这篇关于如何获取方法参数的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:46