Typeクラスなどで使われる型名の多くはコード上での表記と変わりませんが、ジェネリック型では異なる表記が使われます。 例えばコード上ではList<T>
/List(Of T)
と記述される型にはSystem.Collections.Generic.List`1
といった表記が使われます。
型名では山括弧< >
で型引数名を記述する代わりに、バッククオート`
の後ろに型引数の数が記述されます。 例えばDictionary<TKey, TValue>
では型引数の数が2つとなるため、System.Collections.Generic.Dictionary`2
という表記になります。
ジェネリック型における型名の表記
using System;
using System.Reflection;
class Sample {
static void Main()
{
Console.WriteLine(typeof(System.Collections.Generic.List<>));
Console.WriteLine(typeof(System.Collections.Generic.List<int>));
Console.WriteLine(typeof(System.Collections.Generic.Dictionary<,>));
Console.WriteLine(typeof(System.Action));
Console.WriteLine(typeof(System.Action<>));
Console.WriteLine(typeof(System.Action<,>));
Console.WriteLine(typeof(System.Action<,,>));
}
}
実行結果
System.Collections.Generic.List`1[T] System.Collections.Generic.List`1[System.Int32] System.Collections.Generic.Dictionary`2[TKey,TValue] System.Action System.Action`1[T] System.Action`2[T1,T2] System.Action`3[T1,T2,T3]
リフレクションにおいてジェネリック型の名前を文字列で指定する場合には、この表記を使用する必要があります。