Search code examples
c#.netgenericstypestype-parameter

C# get the types defining a Dictionary at run time


I was wondering what is the best way for getting the generic arguments that definine a dictionary at run time is.

Take for example:

Dictionary<string, object> dict;

How at runtime can I find out that the keys are strings?


Solution

  • I'm not sure if I understand your question correctly but I think you mean something like this:

    Dictionary<string, object> dict = new Dictionary<string, object>();
    // ...
    var args = dict.GetType().GetGenericArguments();
    // args[0] will be typeof(string)