可否用字符串类名建立一个类的实例
可不可以用字符串类名建立一个类的实例
如在vb6里creatobject("xxx.xxx")
.net 里要用new xxx.xxx
如果如知道类名xxx.xxx,可不可以和VB6一样建立实例。
------解决方案--------------------
可以
Activator.CreateInstance
------解决方案--------------------
Activator.CreateInstance就行,不过返回的是object类型,需要强制转换。最好定义一个接口或泛型。
public static T Get<T>(string[] param) where T : class
{
return Activator.CreateInstance(typeof(T), param) as T;
}
------解决方案--------------------
要是知道类型,这么写就多此一举了
public static T Get<T>() where T : new()
{
return new T();
}
如在vb6里creatobject("xxx.xxx")
.net 里要用new xxx.xxx
如果如知道类名xxx.xxx,可不可以和VB6一样建立实例。
------解决方案--------------------
可以
Activator.CreateInstance
------解决方案--------------------
Activator.CreateInstance就行,不过返回的是object类型,需要强制转换。最好定义一个接口或泛型。
public static T Get<T>(string[] param) where T : class
{
return Activator.CreateInstance(typeof(T), param) as T;
}
------解决方案--------------------
要是知道类型,这么写就多此一举了
public static T Get<T>() where T : new()
{
return new T();
}