C# リフレクションまとめ5(ジェネリック型を扱う)

Type.GetGenericTypeDefinition

Type.GetGenericArguments

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ReflectionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Type t = typeof(List<String>);
            if (t.IsGenericType)
            {
                //ジェネリック型を取得する
                Type genericType = t.GetGenericTypeDefinition();
                Console.WriteLine(genericType);

                //型引数の型を取得する
                Type[] typeArgTypes = t.GetGenericArguments();
                foreach (Type argType in typeArgTypes)
                {
                    Console.WriteLine(argType);
                }
            }
        }
    }
}