类型数组中的getmethod(methodname,new [] Type)包含ref参数和方法被覆盖

时间:2010-06-26 17:16:26

标签: c#-3.0

例如

class xx
{
    public string name
    {
        get {return "";}
    }
}

class yy
{
    public string name(string n)
    {
         return "";
    }

    public string name(string n,ref string m)
    {
        return "";
    }

    public string name(string n,string m,ref xx k)
    {
        return "";
    }
}

如何通过反射技术获得“名称”方法?

2 个答案:

答案 0 :(得分:1)

使用GetMethod没有简单的方法。但是,您可以使用GetMethods和Linq:

轻松完成此操作
var methodInfo = from m in typeof(yy).GetMethods()
                 where m.Name == "name"
                 let prms = m.GetParameters()
                 where prms.Length == 3
                 && prms[0].ParameterType == typeof(string)
                 && prms[1].ParameterType == typeof(string)
                 && prms[2].ParameterType == typeof(xx).MakeByRefType()
                 select m;

答案 1 :(得分:0)

您可以使用Type.MakeByRefType() - Method创建ref类型参数。其余的由反思类完成。

var method = typeof(yy).GetMethod(
    "name", 
    new[] {typeof(string), typeof(string).MakeByRefType()});