我在實現父/子接口時遇到了問題,因爲它們都是通用接口。我能找到的最佳答案是,這是不可能的,但我也無法找到其他人詢問完全相同的問題。我希望我只是不知道正確的語法,以使編譯器明白我想要做什麼。這裏是我試圖實現的代碼的簡化示例。如何實現具有子通用接口的通用接口
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而沒有錯誤。問題在於祖父母Group試圖實施I_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的定義在哪裏? –
它在那裏,你向下滾動? –
* feelin stupid ... * –