C# - 一個列表中的多個泛型類型 - C# - Multiple generic types in one list

問題:

This is probably not possible, but I have this class:這可能是不可能的,但我有這門課:

public class Metadata<DataType> where DataType : struct
{
    private DataType mDataType;
}

There's more to it, but let's keep it simple.還有更多內容,但讓我們保持簡單。 The generic type (DataType) is limited to value types by the where statement.泛型類型 (DataType) 被 where 語句限制爲值類型。 What I want to do is have a list of these Metadata objects of varying types (DataType).我想要做的是列出這些不同類型(DataType)的元數據對象。 Such as:如:

List<Metadata> metadataObjects;
metadataObjects.Add(new Metadata<int>());
metadataObjects.Add(new Metadata<bool>());
metadataObjects.Add(new Metadata<double>());

Is this even possible?這甚至可能嗎?


解決方案:

參考一: https://en.stackoom.com/question/1Tra
參考二: https://stackoom.com/question/1Tra
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章