如何在给定字典的情况下获取 TKey 和 TValue 的类型<TKey,TValue>类型 [英] How to get type of TKey and TValue given a Dictionary&lt;TKey,TValue&gt; type

查看:26
本文介绍了如何在给定字典的情况下获取 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];

参考:Type.GetGenericArguments

这篇关于如何在给定字典的情况下获取 TKey 和 TValue 的类型<TKey,TValue>类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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