如何在给定字典的情况下获取 TKey 和 TValue 的类型<TKey,TValue>类型 [英] How to get type of TKey and TValue given a Dictionary<TKey,TValue> type
本文介绍了如何在给定字典的情况下获取 TKey 和 TValue 的类型<TKey,TValue>类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在给定 Dictionary
类型的情况下获取 TKey 和 TValue 的类型.
I want to get type of TKey and TValue given a Dictionary<TKey,TValue>
type.
例如.如果类型是 Dictionary
我想知道如何得到keyType = typeof(Int32) 和valueType = typeof(String)
eg. If type is Dictionary<Int32,String>
I want to know how to get
keyType = typeof(Int32) and
valueType = typeof(String)
推荐答案
我想这可能就是你要找的:
I think this may be what you are looking for:
Dictionary<int, string> dictionary = new Dictionary<int, string>();
Type[] arguments = dictionary.GetType().GetGenericArguments();
Type keyType = arguments[0];
Type valueType = arguments[1];
这篇关于如何在给定字典的情况下获取 TKey 和 TValue 的类型<TKey,TValue>类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文