有点困惑,我有一个通用类
public abstract class MyClass<T> : UserControl
{
}
我有这样的类型
Type type = Type.GetType("Type From DB as String", true, true);
我想使用类型创建 MyClass 的实例...但这不起作用。
MyClass<type> control = (MyClass<type>)LoadControl("/UsercControl.ascx");
有任何想法吗????
像这样的东西:
Type typeArgument = Type.GetType("Type From DB as String", true, true);
Type template = typeof(MyClass<>);
Type genericType = template.MakeGenericType(typeArgument);
object instance = Activator.CreateInstance(genericType);
现在你将无法use那作为MyClass<T>
就调用它的方法而言,因为你不知道T
...但是您可以使用一些不需要的方法定义非通用基类或接口T
,然后投射到该位置。或者您可以通过反射调用它的方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)