ジェネリック型では、型引数に具体的な型が指定されているかどうかで型情報が変わります。 たとえばList<T>クラスを例にとった場合、型引数T
の型が具体的に定まっていないList<T>
を特にオープンジェネリック型(あるいはジェネリック型定義)、対してList<int>
やList<string>
など型引数T
の型が具体的な型が定まっているものをクローズジェネリック型(あるいは構築ジェネリック型、構築された型)と呼びます。
typeof
演算子・GetType
演算子でジェネリック型の型情報を取得する場合、オープンジェネリック型とクローズジェネリック型を区別して取得することができます。 オープンジェネリック型を表す場合は型パラメータの指定を省略した型名(例:Dictionary<,>
/Dictionary(Of ,)
)を使用し、一方クローズジェネリック型では具体的な型名を指定した型名(例:Dictionary<string, int>
/Dictionary(Of String, Integer)
)を使用することによってそれぞれの型情報を取得することができます。
オープンジェネリック型およびクローズジェネリック型の型情報を取得する
Imports System
Imports System.Collections.Generic
Class Sample
Shared Sub Main()
Dim t1 As Type = GetType(List(Of)) ' 型パラメータが1つのオープンジェネリック型
Dim t2 As Type = GetType(List(Of Integer)) ' 型パラメータが1つのクローズジェネリック型
Dim t3 As Type = GetType(List(Of String))
Console.WriteLine(t1)
Console.WriteLine(t2)
Console.WriteLine(t3)
Dim t4 As Type = GetType(Dictionary(Of ,)) ' 型パラメータが2つのオープンジェネリック型
Dim t5 As Type = GetType(Dictionary(Of String, Integer)) ' 型パラメータが2つのクローズジェネリック型
Console.WriteLine(t4)
Console.WriteLine(t5)
Dim t6 As Type = GetType(Action(Of ,))
Dim t7 As Type = GetType(Action(Of ,,))
Dim t8 As Type = GetType(Action(Of ,,,))
Console.WriteLine(t6)
Console.WriteLine(t7)
Console.WriteLine(t8)
End Sub
End Class
実行結果
System.Collections.Generic.List`1[T] System.Collections.Generic.List`1[System.Int32] System.Collections.Generic.List`1[System.String] System.Collections.Generic.Dictionary`2[TKey,TValue] System.Collections.Generic.Dictionary`2[System.String,System.Int32] System.Action`2[T1,T2] System.Action`3[T1,T2,T3] System.Action`4[T1,T2,T3,T4]