2012-09-20 74 views
1

我有實體:NBuilder - 如何創建一個包含集合幾個派生類

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

是否有可能建立的BaseEntity列表(集合),但只包含派生類的實例?

感謝

編輯

我的意思是有可能創造使用NBuilder Build<BaseEntity>

回答

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; } 
} 
+0

對不起,我忘了列表在內容上提及NBuilder。請參閱我的編輯。 – bonjorno

相關問題