【问题标题】:Create a dictionary based on the typename in C# [duplicate]根据 C# 中的类型名创建字典 [重复]
【发布时间】:2020-06-13 14:40:24
【问题描述】:

我有一个 Type 变量 - Type t = typeof(myClass);

如何创建字典 - Dictionary<String, myClass> 使用类型变量 t?

【问题讨论】:

  • @PavelAnikhouski 是的,确实如此。将我的问题标记为重复

标签: c# .net reflection


【解决方案1】:

您可以将 Activator.CreateInstance 与泛型类型一起使用:

var genType = typeof(Dictionary<,>).MakeGeneric(typeof(string), t);
var dictionary = Activator.CreateInstance(genType);

【讨论】:

  • 感谢您的回答。正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-15
  • 2021-12-07
  • 1970-01-01
  • 2022-01-24
  • 2019-02-23
  • 2016-01-30
相关资源
最近更新 更多