4
大家好,反思:調用一個具有泛型參數的方法
我在調用帶反射的方法時遇到了一些問題。
方法標識是
public T Create<T, TK>(TK parent, T newItem, bool updateStatistics = true, bool silent = false)
where T : class
where TK : class;
public T Create<T, TK>(TK parent, string newName, Language language = null, bool updateStatistics = true, bool silent = false)
where T : class
where TK : class;
我想使用第二過載。
我的代碼是
typeof(ObjectType).GetMethod("Create")
.MakeGenericMethod(new Type[] { typeof(Item), typeof(TKparent) })
.Invoke(_objectInstance, new object[] { parent, name, _language, true, false });
其中項目是一個類,TKparent是一種類型的可變和父是TKparent實例。
我得到一個System.Reflection.AmbiguousMatchException。
我認爲這個問題是關係到仿製藥
我想也是這個:
typeof(ObjectType).GetMethod("Create", new Type[] { typeof(TKparent), typeof(string), typeof(Globalization.Language), typeof(bool), typeof(bool) })
.MakeGenericMethod(new Type[] { typeof(Item), typeof(Tparent) })
.Invoke(_objectInstance, new object[] { parent, name, _language, true, false });
但在這種情況下,我得到一個System.NullReferenceException(找不到方法)
誰能幫助有了這個,我生氣了!
謝謝