如果我有这样的方法:
public void MyMethod(int arg1, string arg2)
我将如何获取参数的实际名称? 我似乎无法在 MethodInfo 中找到任何实际上会给我参数名称的内容。
我想写一个这样的方法:
public static string GetParamName(MethodInfo method, int index)
所以如果我调用这个方法:
string name = GetParamName(MyMethod, 0)
它将返回“arg1”。这可能吗?
最佳答案
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;
}
上面的示例应该可以满足您的需求。
关于c# - 如何获得方法参数的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/214086/