【发布时间】:2020-05-05 14:21:26
【问题描述】:
我有这种类型约束
where TLookup : ILookupable<ILookup>
我正在为类型传递这个接口
public interface IEmployeeProxy : ILookupableCode
你在哪里
public interface ILookupableCode : ILookupable<ILookupCode>
和
public interface ILookupable<T> where T : ILookup
{
Task<IEnumerable<T>> LookupByNameAsync(string name, int take = 5, int skip = 0);
Task<T> LookupByIdAsync(long id);
}
还有
public interface ILookupCode : ILookup
但是我收到了There is no implicit reference conversion 错误,我不知道为什么,因为对我来说IEmployeeProxy 是
IEmployeeProxy => ILookupableCode => ILookupable<ILookupCode> => ILookupable<ILookup>
我在这里做错了什么?为什么我会收到此错误?对我来说看起来我应该工作。
如果你想知道我为什么要这样做,是因为我希望 ILookupable 有一些返回 ILookup 的方法,但也有 ILookupableCode 和 ILookupable 相同的方法返回ILookupCode 以及更多内容。
【问题讨论】:
-
仅仅因为
T1和T2这两种类型表现出特定的继承/实现关系,并不暗示G<T1>和G<T2>会表现出相同关系。ILookupable<ILookupCode>与ILookupable<ILookup>不相关。有可能,但这取决于接口是否适合协方差/反方差。 -
@Damien_The_Unbeliever 可以更好地解释
ILookupable<ILookupCode>与ILookupable<ILookup>无关?在什么情况下会发生这种情况? -
默认为真。
List<Bird>不是List<Animal>,因为这样有人可以在其中粘贴Giraffe。 -
@Vencovsky Covariance and Contravariance in C#
-
@Vencovsky 可能是this 对你有用。
标签: c# constraints type-constraints