简体   繁体   English

如何获得字典中的类型 <int, string> ?

[英]How can I get the types in Dictionary<int, string>?

The requirement is like this. 要求是这样的。 Now I have a Dictionary. 现在我有字典。

Dictionary<int,string> dic = new Dictionary<int,string>();
//... some other operations
Type t = typeof(Dictionary<int,string>);

Now I want to get the type int and string. 现在,我要获取类型int和字符串。 How can I get the two? 我怎么能得到两个? Thank you very much. 非常感谢你。

Update: 更新:

Thank you for the response. 感谢您的答复。 Actually my requirement is to create another generic class based on the two types, which were got from the dictionary type. 实际上,我的要求是基于这两种类型创建另一个通用类,它们是从字典类型中获得的。 Just like this. 像这样。

PropertyType propertyType = typeof(Dictionary<int,string>);
if (propertyType.Name.Contains("Dictionary"))
{
    Type keyType = propertyType.GetGenericArguments()[0];
    Type valueType = propertyType.GetGenericArguments()[1];
    propertyType = typeof(SerializableDictionary<keyType, valueType>);
}
//And after this, it will dynamically create a class and add the propery as one
//of its properties

Now I cannot use propertyType = typeof(SerializableDictionary<keyType, valueType>) . 现在,我不能使用propertyType = typeof(SerializableDictionary<keyType, valueType>) How should I update this statement? 我应该如何更新此声明? Thank you. 谢谢。

Here is a snippet: 这是一个片段:

Type keyType = t.GetGenericArguments()[0];
Type valueType = t.GetGenericArguments()[1];

Update: 更新:

var typeOfNewDictonary = typeof(SerializableDictionary<,>)
                               .MakeGenericType(new[] 
                                           { 
                                               keyType, 
                                               valueType 
                                           });

BTW: The following line is wrong: 顺便说一句:以下行是错误的:

PropertyType propertyType = typeof(Dictionary<int,string>);

It should be rather: 应该是:

Type propertyType = typeof(Dictionary<int,string>);

What's more, condition in if (propertyType.Name.Contains("Dictionary")) has no sense. 而且, if (propertyType.Name.Contains("Dictionary"))中的条件没有意义。 It always be evaluated as true . 始终将其评估为true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换清单 <string> 到字典 <string, int> ? - How can I transform a List <string> to a Dictionary<string, int>? 如何让 OpenApi Generator 转换字典<int, string>正确吗?</int,> - How can I get OpenApi Generator to convert Dictionary<int, string> correctly? 如何订购字典列表 <int, string> 基于Dictionary int值? - How can I order a List of Dictionary<int, string> based on Dictionary int value? 如何将string []转换为字典 <int, string> 使用单独的int []作为键集? - How can I convert a string[] into a dictionary<int, string> using a separate int[] as the key set? 如果我有字典<string,list<int,int> &gt;如何在 c# 中访问此列表的密钥? </string,list<int,int> - if i have a dictionary<string,list<int,int>>how can i access the key of this list in c#? 如何序列化字典 <int, string> ? - How do I serialize a Dictionary<int, string>? 如何返回字典 <int, object> ? - How can I return a Dictionary<int, object>? 我如何反序列化json字符串列表 <Dictionary<string,int> &gt;使用JQuery? - How can I deserialize a json string List<Dictionary<string,int>> using JQuery? 如何从字典中获取 IWebElement<string, IWebElement> - How can I get IWebElement from dictionary<string, IWebElement> 如何从Dictionary中检索前n个元素 <string, int> ? - How can I retrieve first n elements from Dictionary<string, int>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM