泛型接口
using System; using System.Collections.Generic; using System.Text; namespace 泛型 { class Program { static void Main(string[] args) { //实例化接口 IGenericl<System.ComponentModel.IListSource> factory = new Factory<System.Data.DataTable, System.ComponentModel.IListSource>(); //输出指定泛型的类型 Console.WriteLine(factory.CreateInstance().GetType().ToString()); Console.ReadLine(); } } public interface IGenericl<T> { T CreateInstance();//接口中调用CteateInstance方法 } //实现上面泛型接口的泛型类 //派生约束where T : TI(T要继承自TI) //构造函数约束where T : new() (T 可以实例化) public class Factory<T, TI> : IGenericl<TI> where T : TI,new() { public TI CreateInstance()//创建一个公共方法CreateInstance { return new T(); } } }
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】AI 的力量,开发者的翅膀:欢迎使用 AI 原生开发工具 TRAE
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台