簡體   English   中英

通用類型參數,用於在運行時設置動態類型的列表

[英]Generic type parameter to set a List of a dynamic Type on runtime

我有一個在運行時生成的動態Type ,例如myGeneratedType 我正在嘗試創建以下類的實例,其中T應該是myGeneratedType 我遇到了幾個答案,其中必須將動態類型設置為類的屬性,但似乎無法使其在List<T>

Type myGeneratedType;

private class Container<T>
{
    public string a { get; set; }
    public string b { get; set; }
    public List<T> c { get; set; }
}

void Main()
{
    Container<myGeneratedType> c = new Container<myGeneratedType>(); 
}

顯然,我的方法Main()中的代碼行不起作用。 我應如何創建以我的動態類型作為通用類型參數的Container類?

使用MakeGenericType:

        var type = typeof(Container<>);
        var MyGenericType = type.MakeGenericType(myGeneratedType));

其中myGeneratedTypeType

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM