好吧,我有一节课:
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()中,我想使用GetMethod()实现FillElem()
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()工作得很好。tlist和tset不是null,分别返回"List'1“和"DataSet”。那为什么会发生这种事?
发布于 2016-01-18 05:01:36
试试这个:
//Getting FillElem from tUObject
MethodInfo mi = t.GetMethod("FillElem", BindingFlags.Public | BindingFlags.Instance, null, new[] { tlist.MakeByRefType(), tset }, null);发布于 2016-01-18 05:26:20
回忆器的答案应该有效。以下代码返回正确的数据:
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);

所以你需要寻找另一个问题。您确定您传递的类型是正确的吗?
https://stackoverflow.com/questions/34847470
复制相似问题