Reflection – Getting the generic arguments from a System.Type instance

Use Type.GetGenericArguments. For example:

using System;
using System.Collections.Generic;

public class Test
{
    static void Main()
    {
        var dict = new Dictionary<string, int>();

        Type type = dict.GetType();
        Console.WriteLine("Type arguments:");
        foreach (Type arg in type.GetGenericArguments())
        {
            Console.WriteLine("  {0}", arg);
        }
    }
}

Output:

Type arguments:
  System.String
  System.Int32

Leave a Comment