Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

eg. If type is Dictionary<Int32,String> I want to know how to get keyType = typeof(Int32) and valueType = typeof(String)

like image 829
harsha Avatar asked Mar 08 '10 06:03

harsha


1 Answers

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

like image 147
Fredrik Mörk Avatar answered Sep 30 '22 18:09

Fredrik Mörk