1

我有实体:

BaseEntity (Base Class & Abstract)
DerivedOne (based on BaseEntity)
DerivedTwo (based on BaseEntity)
...

是否可以构建一个列表(集合)BaseEntity但仅包含派生类的实例?

谢谢

编辑

我的意思是可以使用 NBuilder 创建列表Build<BaseEntity>

4

1 回答 1

0

是的,这可能是样本

class Program
{
    static void Main(string[] args)
    {
        List<BaseEntity> list = new List<BaseEntity>();
        DerivedOne d1 = new DerivedOne() { bp1 = 1, Name = "DerivedOne" };
        DerivedTwo d2 = new DerivedTwo() { bp1 = 2, Name = "DerivedTwo" }; 

        list.Add(d1);
        list.Add(d2);

    }
}

public class BaseEntity
{
    public int bp1 { get; set; }
}

public class DerivedOne : BaseEntity
{
    public string Name { get; set; }
}

public class DerivedTwo : BaseEntity
{
    public string Name { get; set; }
}
于 2012-09-20T04:10:03.277 回答