[英]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.