让我举个例子:
-
我有一些通用类/接口定义:
interface IGenericCar< T > {...}
-
我有另一个类/接口,我想与上面的类关联,例如:
interface IGarrage< TCar > : where TCar: IGenericCar< (**any type here**) > {...}
基本上,我希望我的通用 IGarrage 依赖于IGenericCar
,无论是IGenericCar<int>
or IGenericCar<System.Color>
,因为我对该类型没有任何依赖。
通常有两种方法可以实现此目的。
Option1: 添加另一个参数IGarrage
代表着T
应该传递到IGenericCar<T>
约束:
interface IGarrage<TCar,TOther> where TCar : IGenericCar<TOther> { ... }
Option2:定义一个基本接口IGenericCar<T>
这不是通用的并且对该接口有限制
interface IGenericCar { ... }
interface IGenericCar<T> : IGenericCar { ... }
interface IGarrage<TCar> where TCar : IGenericCar { ... }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)