C#泛型的实例化
方法一
在使用泛型的类后面加入一个 where T : new()
public class Example<T> where T : new()
{
public static T Get()
{
T t = new T();
...........
}
}
方法二
可以使用 System.Activator.CreateInstance<T>()
创建泛型实例对像
public class Example<T>
{
public static T Get()
{
T t = System.Activator.CreateInstance<T>();
.....
}
}
- Link : https://zdyla.com/post/csharp-generic-create-instance.html
- Copyright Notice : Unless otherwise stated, please contact the author for authorization and indicate the source!