1
static class Extensions
{
public static string Primary<T>(this T obj)
{
Debug.Log(obj.ToString());
return "";
}
public static string List<T>(this List<T> obj)
{
Debug.Log(obj.ToString());
return "";
}
}
使用反射來調用這兩個擴展方法如何使用C#反射調用具有通用List參數的擴展方法?
//This works
var pmi = typeof(Extensions).GetMethod("Primary");
var pgenerci = pmi.MakeGenericMethod(typeof(string));
pgenerci.Invoke(null, new object[] {"string" });
//This throw a "ArgumentException: failed to convert parameters"
var mi = typeof(Extensions).GetMethod("List");
var stringGeneric = mi.MakeGenericMethod(typeof(List<string>));
stringGeneric.Invoke(null, new object[] {new List<string> { "list of string"}, });
我與Unity3d工作,所以.NET版本是3.5