Dog class "cat" -> Cat class 所以我试过了 public Dictionary dic = new D-6ren">
gpt4 book ai didi

c# - 如何使用像字典 这样的强类型将字符串关联到类

转载 作者:太空狗 更新时间:2023-10-29 23:58:32 25 4
gpt4 key购买 nike

我想创建一个字典来将一个字符串关联到一个类,例如:

"dog" -> Dog class
"cat" -> Cat class

所以我试过了

public Dictionary<String, Type> dic = new Dictionary<String, Type>();
dic.Add("dog", Dog);

但是 Add 不接受 Dog

那么语法是什么?

最佳答案

你应该使用 typeof运算符获取相应的 Type 对象:

public Dictionary<String, Type> dic = new Dictionary<String, Type>();
dic.Add("dog", typeof(Dog));

关于c# - 如何使用像字典 <string,Type> 这样的强类型将字符串关联到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3751131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com