GetMethod返回null

时间:2016-01-18 04:45:11

标签: c# .net system.reflection

好的,我有这个课程:

public class tUObject
{
    public const string strSelect = "SELECT Id, cName, Major FROM tUObject WHERE Id = ";

    //constructors and stuff...

    public virtual void FillElem(ref List<tUObject> l, DataSet ds)
    {
        //...
    }
}

MainWindow()我希望使用FillElem()转到GetMethod()

static List<tUObject> obj = new List<tUObject>();
static DataSet objDataSet = new DataSet();
//...
string strClass = objDataSet.Tables[0].Rows[0]["Class"].ToString(); //"tUObject"
Type t = Type.GetType("UniDB_WPF." + strClass);  //tUObject with the assembly name
Type tlist = obj.GetType();                      //list<tUObject>
Type tset = objDataSet.GetType();                //DataSet

//Getting strSelect from tUObject
string strAccessSelect = t.GetField("strSelect").GetRawConstantValue().ToString() + ((int)item.Tag).ToString();
//...
//Getting FillElem from tUObject
MethodInfo mi = t.GetMethod("FillElem", BindingFlags.Public, null, new[] { tlist, tset }, null);

问题是,GetMethod()返回null,而GetField()完美无缺。 tlisttset不为空并返回&#34;列表&#39; 1&#34;和&#34; DataSet&#34;分别。那么为什么会这样呢?

2 个答案:

答案 0 :(得分:2)

试试这个:

//Getting FillElem from tUObject
MethodInfo mi = t.GetMethod("FillElem", BindingFlags.Public | BindingFlags.Instance, null, new[] { tlist.MakeByRefType(), tset }, null);

答案 1 :(得分:2)

Memoizer的答案应该有效。 以下代码返回正确的数据:

public class Test
{
    public virtual void Test1(ref List<object> t1, object t2)
    {

    }
}

和方法请求

var t = new Test();
var mi = t.GetType().GetMethod("Test1", BindingFlags.Instance | BindingFlags.Public, null, new Type[] { typeof(List<object>).MakeByRefType(), typeof(object) }, null);

enter image description here

所以你需要搜索另一个问题。你确定你传递的是正确的类型吗?