C# 超类型集合引用引用的子类型集合对象的编译时错误

C# 超类型集合引用引用的子类型集合对象的编译时错误,c#,generics,inheritance,compiler-errors,polymorphism,C#,Generics,Inheritance,Compiler Errors,Polymorphism,我设计了实体类,这样可以在基类中设置抽象,因为我不想重复相同的代码。然而,在这样做的过程中,我已经达到了一个点,我想让超类的泛型集合的引用指向子类类型的集合,但它会抛出一个编译器错误。请参考下面的课程大纲,并建议如何重新设计或解决问题,以便在测试方法中不会出现任何错误 请注意,如果您的解决方案是像我在baseRef3中所做的那样,那么这不是我想要的。我希望基集合类引用指向类型化的子类集合对象 public interface IEntity { } public class BaseEntit

我设计了实体类,这样可以在基类中设置抽象,因为我不想重复相同的代码。然而,在这样做的过程中,我已经达到了一个点,我想让超类的泛型集合的引用指向子类类型的集合,但它会抛出一个编译器错误。请参考下面的课程大纲,并建议如何重新设计或解决问题,以便在测试方法中不会出现任何错误

请注意,如果您的解决方案是像我在baseRef3中所做的那样,那么这不是我想要的。我希望基集合类引用指向类型化的子类集合对象

public interface IEntity
{
}

public class BaseEntity : IEntity
{
}

public abstract class BaseEntityCollection<T> : List<T>
    where T : BaseEntity, new()
{
}

public class RealEntity : BaseEntity
{
}

public class RealEntityCollection : BaseEntityCollection<RealEntity>
{
}

public static void Test()
    {
        //Gives compile time error on both of below lines
        BaseEntityCollection<BaseEntity> baseRef1 = new RealEntityCollection();
        BaseEntityCollection<BaseEntity> baseRef2 = (BaseEntityCollection<RealEntity>)(new RealEntityCollection());

        //Following works with RealEntity in the refence but I do not want to keep ref to subtype rather base
        BaseEntityCollection<RealEntity> baseRef3 = new RealEntityCollection();         
    }
公共接口的可扩展性
{
}
公共类BaseEntity:
{
}
公共抽象类BaseEntityCollection:列表
其中T:BaseEntity,new()
{
}
公共类RealEntity:BaseEntity
{
}
公共类RealEntityCollection:BaseEntityCollection
{
}
公共静态无效测试()
{
//在下面两行上都给出编译时错误
BaseEntityCollection baseRef1=新的RealEntityCollection();
BaseEntityCollection baseRef2=(BaseEntityCollection)(新的RealEntityCollection());
//下面在引用中使用RealEntity,但我不想将ref保留为subtype而不是base
BaseEntityCollection baseRef3=新的RealEntityCollection();
}
谢谢

编辑:我使用的是.NET3.5,所以不确定是否支持泛型协方差。非常感谢您的帮助


我将我的设计进一步简化为以下内容:

public interface IEntity
{
}

public class BaseEntity : IEntity
{
}

public class RealEntity : BaseEntity
{
}

public class RealEntityCollection : IEnumerable<RealEntity>
{
}
公共接口的可扩展性
{
}
公共类BaseEntity:
{
}
公共类RealEntity:BaseEntity
{
}
公共类RealEntityCollection:IEnumerable
{
}
但以下两行仍然给出编译器错误:

IEnumerable<BaseEntity> baseRef1 = new RealEntityCollection();
IEnumerable<IEntity> baseRef2 = new List<RealEntity>();
IEnumerable baseRef1=新的RealEntityCollection();
IEnumerable baseRef2=新列表();
然而,以下两个方面正在发挥作用:

IEnumerable<BaseEntity> baseRef3 = (IEnumerable<BaseEntity>)(new List<RealEntity>());
IEnumerable<IEntity> baseRef4 = (IEnumerable<IEntity>)(new List<RealEntity>());
IEnumerable baseRef3=(IEnumerable)(new List());
IEnumerable baseRef4=(IEnumerable)(新列表());

事实上,我不想有铸造作为它的时间消耗,所以上述两行工作似乎并不理想。在.NET 3.5中有没有其他方法可以解决这个问题?

A
BaseEntityCollection
不是
BaseEntityCollection
。否则,您可以这样做:

BaseEntityCollection<BaseEntity> baseRef1 = new RealEntityCollection();
baseRef1.Add(new OtherRealEntity());
BaseEntityCollection baseRef1=新的RealEntityCollection();
baseRef1.Add(新的OtherRealEntity());

除非您将
BaseEntityCollection
的定义更改为使用协变接口,如
IEnuemerable
,否则无法强制转换/伪造编译器。

关于这一点有很多问题。你要问的是类和值类型不支持的“通用协方差”。我使用的是.NET3.5,如果我使用接口,那么它会被支持吗?您建议重新设计的代码应该是什么样子?谢谢!OP确认他正在使用.Net3.5。我相信c#4.0支持协方差,但不确定.Net3.5是否支持c#4.0编译器。