一、在使用泛型的类后面加入一个 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     }

 

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

posted on 2013-03-22 17:09  itprobie-菜鸟程序员  阅读(281)  评论(0编辑  收藏  举报