gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:09:07 26 4
gpt4 key购买 nike

如果我有这样的方法:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com