C# 创建对象的方法

时间:2021-06-20 14:18:50

1.实例化方法,也就是new();

//where T:new () 表示T必须有构造函数
public static T Create<T> where T:new ()
{
return T();
}

2.Activator创建实例;

public static T Act_Create<T>()
{
return Activator.CreateInstance<T>();
}

3.反射创建实例;

//strPath:.dll 路径,strName 类名
public static T Ass_Create<T>()
{
return (T)Assembly.Load(strPath).CreateInstance(strName);
}

其中:第1,2个效率高,第3个由于是反射,效率略低。