开发者

How to get type of TKey and TValue given a Dictionary<TKey,TValue> type

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

eg. If type is Dictionary<Int32,Stri开发者_开发百科ng> 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];

Ref: Type.GetGenericArguments

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜