C# 具有泛型数组的泛型接口

C# 具有泛型数组的泛型接口,c#,generics,C#,Generics,假设我们有以下接口: public interface ILine<T> { T Item { get; set;} } 现在我有课了 public class Lines1 { public Line1[] Items { get; set;} } 我想创建一个接口 public interface ILines<T> { ILine<T>[] Items { get; set;} } 公共接口线 { ILine[]项{get;

假设我们有以下接口:

public interface ILine<T>
{
    T Item { get; set;}
}
现在我有课了

public class Lines1
{
    public Line1[] Items { get; set;}
}
我想创建一个接口

public interface ILines<T>
{
    ILine<T>[] Items { get; set;}
}
公共接口线
{
ILine[]项{get;set;}
}
让Lines1实现它
Lines1:ILines
(和
Lines2
Item2
执行相同的操作)。但这是不可能的,因为编译器错误

“Lines1”未实现接口成员“ILines.Items”。 “Lines1.Items”无法实现“ILines.Items”,因为它确实实现了 没有匹配的返回类型“IEnumerable>”

这些类是从xml反序列化的,因此我确实需要属性作为
Item1
Item2
Line1
Line2
,而不是作为接口。 我该怎么做才能继续前进?或者我正在尝试应用一些著名的反模式

更新:非常好的解释,为什么不可能:

不幸的是,在写问题之前我还没有找到它。

ILine[]
Line1[]
不同。关于方差的概念有很多答案,他们可以清楚地解释这一点

解决方案是确保类型实际上相互映射。一种方法是将
Line1
也设为通用参数:

interface ILines<T, U> where U : ILine<T>
{
  U[] Items { get; set; }
}
接口线,其中U:ILine
{
U[]项{get;set;}
}
遗憾的是,这意味着您丢失了一些类型推断,因此它可能意味着相当多的额外文本。

您可能需要检查一下。
You may want to check this out.    

    public class LinesOne : ILines<Item1>
        {
            public ILine<Item1>[] Lines
            {
                get
                {
                    throw new NotImplementedException();
                }

                set
                {
                    throw new NotImplementedException();
                }
            }
        }

        public class LinesTwo : ILines<Item2>
        {
            public ILine<Item2>[] Lines
            {
                get
                {
                    throw new NotImplementedException();
                }

                set
                {
                    throw new NotImplementedException();
                }
            }
        }
        public interface ILines<T>
        {

            ILine<T>[] Lines { get; set; }
        }

        public interface ILine<T>
        {
            T Item { get; set; }
        }

        public class Item1
        {
        }

        public class Item2
        {
        }

        public class Line1 : ILine<Item1>
        {
            public Item1 Item { get; set; }
        }

        public class Line2 : ILine<Item2>
        {
            public Item2 Item { get; set; }
        }
公共类LinesOne:ILines { 公共线路 { 得到 { 抛出新的NotImplementedException(); } 设置 { 抛出新的NotImplementedException(); } } } 公共类行两个:行 { 公共线路 { 得到 { 抛出新的NotImplementedException(); } 设置 { 抛出新的NotImplementedException(); } } } 公共接口线 { ILine[]行{get;set;} } 公共接口线 { T项{get;set;} } 公共类项目1 { } 公共类项目2 { } 公共类第1行:ILine { 公共项1项{get;set;} } 公共类第2行:ILine { 公共项2项{get;set;} }
这不是我想要实现的目标。您建议使用public-Line1[]行,我需要public-Line1[]项{get;set;}。创建对象的序列化程序知道Line1类是什么,但无法创建接口实例。
You may want to check this out.    

    public class LinesOne : ILines<Item1>
        {
            public ILine<Item1>[] Lines
            {
                get
                {
                    throw new NotImplementedException();
                }

                set
                {
                    throw new NotImplementedException();
                }
            }
        }

        public class LinesTwo : ILines<Item2>
        {
            public ILine<Item2>[] Lines
            {
                get
                {
                    throw new NotImplementedException();
                }

                set
                {
                    throw new NotImplementedException();
                }
            }
        }
        public interface ILines<T>
        {

            ILine<T>[] Lines { get; set; }
        }

        public interface ILine<T>
        {
            T Item { get; set; }
        }

        public class Item1
        {
        }

        public class Item2
        {
        }

        public class Line1 : ILine<Item1>
        {
            public Item1 Item { get; set; }
        }

        public class Line2 : ILine<Item2>
        {
            public Item2 Item { get; set; }
        }