C# 创建一个泛型接口
代码说明:
C# 创建一个泛型接口的例子,创建一个泛型接口的代码和方法如下: public interface IGenericInterface { T CreateInstance(); //接口中调用CreateInstance方法 } //实现上面泛型接口的泛型类 //派生约束where T : TI(T要继承自TI) //构造函数约束where T : new()(T可以实例化) public class Factory : IGenericInterface where T : TI, new() { public TI CreateInstance()//创建一个公共方法CreateInstance { return new T(); } } class Program { static void Main(string[] args) { //实例化接口 IGenericInterface factory = Factory(); //输出指定泛型的类型 Console.WriteLine(factory.CreateInstance().GetType().ToString()); Console.ReadLine(); } }
下载说明:请别用迅雷下载,失败请重下,重下不扣分!


