我想要获得TKey和TValue的类型,给出一个字典< TKey,TValue>类型.
例如.如果类型为Dictionary< Int32,String>我想知道如何得到
keyType = typeof(Int32)和
valueType = typeof(String)
解决方法
我认为这可能是你正在寻找的:
- Dictionary<int,string> dictionary = new Dictionary<int,string>();
- Type[] arguments = dictionary.GetType().GetGenericArguments();
- Type keyType = arguments[0];
- Type valueType = arguments[1];