更改子类属性的类型

c#

我不确定我将如何做到这一点。我有一个通用基类,它是一个 DTO,它具有我需要的大部分属性。这个类有一些也是基类的属性。

我希望能够根据情况拥有其他属性:

例如

 public class BaseOrder
 {
     //Other common properties
     public List<BaseItem> Items { get; set; }
 }
 
 public class BaseItem
 {
     //Properties to do with an Item
 }
 
 public class AdminOrder: BaseOrder
 {
     // THIS IS THE KEY PART: I want Items to be a list of type AdminItem
     public List<AdminItem> Items { get; set; }
 }
 
 public class AdminItem:BaseItem
 {
     //Properties to do with an Item AND Admin stuff too
 }

我怎样才能做到这一点?

回答

public class BaseOrder<TYPE> where TYPE: BaseItem
 {
     //Other common properties
     public List<TYPE> Items { get; set; }
 }
 
 public class BaseItem
 {
     //Properties to do with an Item
 }
 
 public class AdminOrder: BaseOrder<AdminItem>
 {
 }
 
 public class AdminItem:BaseItem
 {
     //Properties to do with an Item AND Admin stuff too
 }


以上是更改子类属性的类型的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>