获取方法的参数值,而不考虑其数量或类型C#

获取方法的参数值,而不考虑其数量或类型C#,c#,methods,parameters,methodinfo,C#,Methods,Parameters,Methodinfo,我想得到任何方法的参数值,即使它们使用C时有不同数量的参数# 例如,如果您有以下3种方法: public void method1 (string param1) { List<string> parametervalues = getParameters(this.Method); } public void method2 (int param1, string param2) { List<string> parametervalues = getP

我想得到任何方法的参数值,即使它们使用C时有不同数量的参数# 例如,如果您有以下3种方法:

public void method1 (string param1)
{
    List<string> parametervalues = getParameters(this.Method);
}
public void method2 (int param1, string param2)
{
    List<string> parametervalues = getParameters(this.Method);
}
public void method3 (string param1, int param2, bool param3)
{
    List<string> parametervalues = getParameters(this.Method);
}
public void方法1(字符串参数1)
{
List parametervalues=getParameters(this.Method);
}
公共void方法2(int-param1,string-param2)
{
List parametervalues=getParameters(this.Method);
}
公共void方法3(字符串参数1、int参数2、bool参数3)
{
List parametervalues=getParameters(this.Method);
}
因此,在getParameters方法中,它将采用methodname并以字符串列表的形式返回参数值,而不考虑参数的数量

我达到了一个点,我可以得到参数的数量,但没有如下值

var method = MethodInfo.GetCurrentMethod();
var parameters = method.GetParameters();
List<string> parameterList = new List<string>();
if (parameters.Length > 0)
{
    foreach (var p in parameters)
    {
        parameterList.Add(p.Name);
    }
}
method1(string a, string b, string c)
{
list<string> = getParameterasList(a,b,c);
}

getParameterasList(params object[] values)
{
            List<string> parameterList = new List<string>();
            if (values.Length > 0)
            {
                foreach (var v in values)
                {
                    parameterList.Add(v.ToString());
                }
            }
return parameterList;
}
var method=MethodInfo.GetCurrentMethod();
var parameters=method.GetParameters();
列表参数List=新列表();
如果(parameters.Length>0)
{
foreach(参数中的var p)
{
参数列表。添加(p.Name);
}
}
更新:

我找到了另一种方法,使用(params object[]value)作为方法参数,如下所示

var method = MethodInfo.GetCurrentMethod();
var parameters = method.GetParameters();
List<string> parameterList = new List<string>();
if (parameters.Length > 0)
{
    foreach (var p in parameters)
    {
        parameterList.Add(p.Name);
    }
}
method1(string a, string b, string c)
{
list<string> = getParameterasList(a,b,c);
}

getParameterasList(params object[] values)
{
            List<string> parameterList = new List<string>();
            if (values.Length > 0)
            {
                foreach (var v in values)
                {
                    parameterList.Add(v.ToString());
                }
            }
return parameterList;
}
method1(字符串a、字符串b、字符串c)
{
列表=GetParametersList(a、b、c);
}
getParameterasList(参数对象[]值)
{
列表参数List=新列表();
如果(value.Length>0)
{
foreach(值中的var v)
{
parameterList.Add(v.ToString());
}
}
返回参数列表;
}

您可以使用。警告:完成、理解和正确实施这些步骤需要时间。一旦您了解了拦截调用的基本知识并能够实现它,您将能够获得方法参数的值

简而言之,拦截是拦截对方法的调用的过程,在调用之前做一些你想做的事情,然后让调用实际发生,然后在返回给调用方的过程中有机会对调用做一些事情

在这里很难向您展示一个完整的示例,但遵循该链接,您会很好。在拦截过程中,当从方法返回异常时,我们就是这样获得方法参数的值的。(您将有权访问
IMethodCallMessage
,其中包含您需要的信息。)我们这样做是为了记录方法参数:

    private static List<ParameterInformation> GetParameterInfoList(IMethodCallMessage methodCallMessage)
    {
        var = new List<ParameterInformation>();

        // Note: This works even if a parameter's value is null.
        for(int i = 0 ; i < methodCallMessage.ArgCount ; i++)
        {
            parameterInformationList.Add(new ParameterInformation(methodCallMessage.GetArgName(i), methodCallMessage.Args[i]));
        }

        return parameterInformationList;
    }
私有静态列表GetParameterInfo列表(IMethodCallMessage methodCallMessage)
{
var=新列表();
//注意:即使参数的值为null,此选项也有效。
对于(int i=0;i
您无法通过反射获取参数值。这是使用反射以外的另一种方式获取值的方法吗?据我所知,不是。您发布的链接指向“MSDN杂志问题”,这在4.5年前是没有的。