所以我有例如這個類
public abstract class Feature
{
public string Id { get; set; }
public string Type { get; set; }
}
然后我將擁有不同的類,它們都繼承了這個,但在其他方面有所不同
所以舉個例子,我會有這個
public class FirstClass: Feature
{
public string FirstClassKey { get; set; }
}
然后我有另一個類,它有一個List<>我想在其中擁有FirstClassKey和所有 Feature 屬性,但它需要是通用的,因為如果我有一個類似SecondClass的也將繼承Feature類,我希望能夠使用下面的類一個 List 將具有SecondClassKey然后是所有 Feature 屬性。
public class FeatureCollection
{
public string Type { get; set; }
public List<> Features { get;set; }
}
所以有時一個FeatureCollection物體看起來像
{
Type: "SomeType",
Features = [FirstClassKey: "12", Id: "20", Type: "FeatureType"]
}
有時喜歡
{
Type: "SomeType",
Features = [SecondClassKey: "48", Id: "20", Type: "FeatureType"]
}
這是可能的還是我必須FeatureCollection為每個將繼承Feature抽象類的不同類創建一個類
uj5u.com熱心網友回復:
我不確定我是否正確理解您想要完成的任務,但在我看來您想要FeatureCollection通用:
public class FeatureCollection<TFeature> where TFeature : Feature
{
public string Type { get; set; }
public List<TFeature> Features { get; set; }
}
現在FeatureCollection<FirstClass>只能存盤FirstClass派生自的型別的實體或實體FirstClass,雖然FeatureCollection<SecondClass>不能存盤FirstClass,但可以存盤SecondClass...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/483193.html
上一篇:在C#中查找樹項
下一篇:自定義型別的鍵值對
