【发布时间】:2013-09-21 21:07:55
【问题描述】:
当它们都是通用的时,我在实现父/子接口时遇到问题。我能找到的最佳答案是不可能,但我也找不到其他人问完全相同的问题。我希望我只是不知道正确的语法来让编译器理解我想要做什么。这是我尝试实现的代码的精简示例。
public interface I_Group<T>
where T : I_Segment<I_Complex>
{
T Segment { get; set; }
}
public interface I_Segment<T>
where T : I_Complex
{
T Complex { get; set; }
}
public interface I_Complex
{
string SomeString { get; set; }
}
public partial class Group : I_Group<Segment>
{
private Segment segmentField;
public Group() {
this.segmentField = new Segment();
}
public Segment Segment {
get {
return this.segmentField;
}
set {
this.segmentField = value;
}
}
}
public partial class Segment : I_Segment<Complex> {
private Complex complexField;
public Segment() {
this.complexField = new Complex();
}
public Complex Complex {
get {
return this.c_C001Field;
}
set {
this.c_C001Field = value;
}
}
}
public partial class Complex : I_Complex {
private string someStringField;
public string SomeString {
get {
return this.someStringField;
}
set {
this.someStringField = value;
}
}
}
所以在这里,Complex 是孙子,它实现了 I_Complex 没有错误。 Segment 是它的父级,它实现 I_Segment 没有错误。问题在于试图实现 I_Group 的祖父母 Group。我得到了错误
The type 'Segment' cannot be used as type parameter 'T' in the generic type or method 'I_Group<T>'. There is no implicit reference conversion from 'Segment' to 'I_Segment<I_Complex>'.
我被引导相信这是协方差的问题,但我也被引导相信这是应该在 C# 4.0 中工作的东西。当孩子不是通用的时,这有效,这使我认为必须存在一些语法才能使其正确编译。难道我做错了什么?这甚至可能吗?如果没有,有人可以帮我理解为什么不?
【问题讨论】:
-
您对 Segment 的定义在哪里?
-
它在那里,你向下滚动了吗?
-
如果你这样写段会发生什么:
public partial class Segment : I_Segment<I_Complex> -
请不要在
I和名称之间写下划线,这是一种糟糕的编程风格。 -
@Chiel92 虽然这确实可以编译,但它要求将成员更改为接口类型,而不是显式的类类型。这最终破坏了我已经编写的使用这些类的代码。
标签: c# c#-4.0 generics interface