如何获得给予解释和LT TKEY的和TValue的类型; TKEY的,TValue>类型 [英] How to get type of TKey and TValue given a Dictionary<TKey,TValue> type

查看:144
本文介绍了如何获得给予解释和LT TKEY的和TValue的类型; TKEY的,TValue>类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给TKEY的和TValue的类型词典< TKEY的,TValue方式> 键入

I want to get type of TKey and TValue given a Dictionary<TKey,TValue> type.

例如。如果类型为词典<的Int32,字符串> 我想知道如何获得
关键字类型= ty​​peof运算(Int32)已和
VALUETYPE = typeof运算(字符串)

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];



参考:的 Type.GetGenericArguments

这篇关于如何获得给予解释和LT TKEY的和TValue的类型; TKEY的,TValue&GT;类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆