3
我想使用的MethodInfo MakeGenericMethod如下方法:C#反射,使用MakeGenericMethod与具有 '新的()' 类型的约束
foreach (var type in types)
{
object output = null;
var method = typeof (ContentTypeResolver).GetMethod("TryConstruct");
var genmethod = method.MakeGenericMethod(type);
var arr = new object[] { from, output };
if ((bool)genmethod.Invoke(null, arr))
return (IThingy)arr[1];
}
对下列通用方法:
public static bool TryConstruct<T>(string from, out IThingy result) where T : IThingy, new()
{
var thing = new T();
return thingTryConstructFrom(from, out result);
}
我遇到的问题是,我在MakeGenericMethod行上得到一个参数异常,因为我传递的类型不是'new()'
有没有办法绕过这个? 谢谢
很好的回答,谢谢! – Dve 2010-03-03 22:26:06