Luk*_*ust 31 .net c# reflection
如果我有一个方法如:
public void MyMethod(int arg1, string arg2)
Run Code Online (Sandbox Code Playgroud)
我如何获得参数的实际名称?我似乎无法在MethodInfo中找到任何实际上会给我参数名称的内容.
我想写一个看起来像这样的方法:
public static string GetParamName(MethodInfo method, int index)
Run Code Online (Sandbox Code Playgroud)
所以如果我用以下方法调用此方法:
string name = GetParamName(MyMethod, 0)
Run Code Online (Sandbox Code Playgroud)
它将返回"arg1".这可能吗?
Tom*_*son 59
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;
}
Run Code Online (Sandbox Code Playgroud)
以上样本应该做你需要的.
尝试这样的事情:
foreach(ParameterInfo pParameter in pMethod.GetParameters())
{
//Position of parameter in method
pParameter.Position;
//Name of parameter type
pParameter.ParameterType.Name;
//Name of parameter
pParameter.Name;
}
Run Code Online (Sandbox Code Playgroud)