无法在 C# 中使用协变/逆变
abstract class A<T> {
List<T> Items { get; set; }
}
class B {}
class C : A<B> {}
class D : B {}
class E : A<D> {}
static class X {
public A<B> GetThing(bool f) {
return f ? new E() : new C();
}
}
无法确定条件表达式的类型,因为“ConsoleApplication4.E”和“ConsoleApplication4.C”之间没有隐式转换
现在我明白了“为什么”(我认为),但我不知道如何进行编译。我想我必须创建一个接口来定义某种差异并从中继承,但我不确定。但无论如何,E()
应该继承自 C()
。
有接受者吗?
TIA
abstract class A<T> {
List<T> Items { get; set; }
}
class B {}
class C : A<B> {}
class D : B {}
class E : A<D> {}
static class X {
public A<B> GetThing(bool f) {
return f ? new E() : new C();
}
}
Type of conditional expression cannot be determined because there is no implicit conversion between 'ConsoleApplication4.E' and 'ConsoleApplication4.C'
Now I get the "why" (I think) but I can't see how to make this compile. I think I have to create an interface which defines variance of some kind and inherit from that, but I'm not sure. But regardless, E()
should inherit from C()
.
Any takers?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要在 C# 中使用泛型变体,您必须满足以下全部条件:
您的程序满足条件 1 和条件 5,但不满足条件 2、3 或 4。
无法获得所需的方差,因为您想要的东西会违反条件 4。观察当我们满足除条件 4 之外的所有条件时会发生什么:
长颈鹿集合现在包含包含老虎的动物列表。
如果要使用方差,则必须是类型安全的。由于编译器无法确定方差注释是否是类型安全的,因此它会拒绝 IMyCollection 的声明。
让我们看看如何做到这一点并保持类型安全。 问题是 Items 可以通过接口进行写入。
完全类型安全。这将是一个合法的 C# 4 程序。
如果您对 C# 4 中协变和逆变的设计细节感兴趣,您可以考虑阅读我关于该主题的十几篇文章。您可以在这里找到它们:
http://blogs.msdn。 com/b/ericlippert/archive/tags/covariance+and+contravariance/
请注意,这些是按从最近到最近的顺序列出的;从底部开始。
如果您特别对确定接口注释何时有效的规则感兴趣,请参阅这篇文章(我刚刚发现并修复了其中的一些错误,所以我很高兴我们进行了这次对话。)
http://blogs.msdn.com/b/ ericlippert/archive/2009/12/03/exact-rules-for-variance-validity.aspx
To use generic variance in C# you have to meet all the following conditions:
Your program meets condition 1 and condition 5 but not conditions 2, 3, or 4.
There is no way to get the variance you want because you want something that would violate condition 4. Watch what happens when we meet all the conditions except condition 4:
And a giraffe collection now contains a list of animals that contains a tiger.
You have to be typesafe if you're going to use variance. Because the compiler cannot determine the variance annotations to be typesafe, it rejects the declaration of IMyCollection.
Let's see how we might do this and be typesafe. The problem is that Items is writable via the interface.
Perfectly typesafe. This would be a legal C# 4 program.
If the design details of covariance and contravariance in C# 4 interests you, you might consider reading my dozen articles on the subject. You can find them here:
http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/
Note that these are listed in most-to-least-recent order; start from the bottom.
If in particular you are interested in the rules for determining when an interface's annotations are valid, see this article (which I just discovered and fixed some errors in, so I'm glad we had this conversation.)
http://blogs.msdn.com/b/ericlippert/archive/2009/12/03/exact-rules-for-variance-validity.aspx
E
和C
之间没有关系。因此,您的返回类型必须是A
,它可以被视为这些类型的公共父级。此外,A
需要是一个接口,其中通用参数是使用out
定义的,这样才能正常工作:无法使其与 三元运算符 (
?:
)。编译器根本不喜欢它。更新:
在 @Eric Lippert 在评论部分的精彩评论之后,您可以通过转换为
A
来使用三元运算符:There is no relation between
E
andC
. So your return type must beA<B>
which may be considered as a common parent to those types. AlsoA
needs to be an interface where the generic argument is defined without
for this to work:Couldn't make it work with the ternary operator (
?:
). The compiler simply doesn't like it.UPDATE:
After @Eric Lippert's excellent remark in the comments section you could use the ternary operator by casting to
A<B>
:这是行不通的,因为
C
和E
没有共同的父级。对于这样的事情,您需要创建一个通用的父类型,无论是类还是接口。This cannot work because
C
andE
have no common parent. To something like this you need to create a common parent type, be it a class or an interface.