泛型实例化 itprobie

一、在使用泛型的类后面加入一个 where T : new(),代码如下:

View Code
1     public class A<T> where T : new()
2     {
3         public static T Get()
4         {
5             T obj = new T();
6             return obj;
7         }
8     }

二、可以使用System.Activator.CreateInstance<T>()创建泛型实例对像。代码如下:

View Code
1     public class A<T>
2     {
3         public static T Get()
4         {
5             T obj = System.Activator.CreateInstance<T>();
6             return obj;
7         }
8     }

 程序员的基础教程:菜鸟程序员

原文地址:https://www.cnblogs.com/guohu/p/2975962.html