【问题标题】:Given the names of the types as strings, how would I construct a generic using reflection? [duplicate]给定类型的名称作为字符串,我将如何使用反射构造泛型? [复制]
【发布时间】:2009-03-28 02:45:26
【问题描述】:

假设我有字符串“List”和“Socket”,我将如何创建 List

我需要的答案同样适用于 Queue 和 XmlNodeList,更不用说 MyCustomGeneric 和 MyCustomClass。

【问题讨论】:

    标签: c# generics reflection


    【解决方案1】:
    Type t=Type.GetType("System.Collections.Generic.List`1[System.Net.Sockets.Socket]");
    object o=Activator.CreateInstance(t);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-17
      相关资源
      最近更新 更多