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

 

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

相关文章:

  • 2021-10-26
  • 2022-12-23
  • 2021-06-02
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-11
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
相关资源
相似解决方案