ジェネリック型に関する情報は、他の型に関する情報と同じ方法で取得されます。ジェネリック型を表す Type オブジェクトを調べることです。 原則上の違いは、ジェネリック型には、そのジェネリック型パラメーターを表す Type オブジェクトの一覧があるということです。 このセクションの最初の手順では、ジェネリック型を調べます。
ジェネリック型定義の型パラメーターに型引数をバインドすることで、構築された型を表す Type オブジェクトを作成できます。 2 番目の手順でこれを示します。
ジェネリック型とその型パラメーターを調べるには
ジェネリック型を表す Type のインスタンスを取得します。 次のコードでは、C#
typeof
演算子 (Visual Basic でGetType
) を使用して型を取得します。 Type オブジェクトを取得するその他の方法については、「Type」を参照してください。 このプロシージャの残りの部分では、型はt
という名前のメソッド パラメーターに含まれています。Type d1 = typeof(Dictionary<,>);
Dim d1 As Type = GetType(Dictionary(Of ,))
IsGenericType プロパティを使用して型がジェネリックかどうかを判断し、IsGenericTypeDefinition プロパティを使用して型がジェネリック型定義であるかどうかを判断します。
Console.WriteLine($" Is this a generic type? {t.IsGenericType}"); Console.WriteLine($" Is this a generic type definition? {t.IsGenericTypeDefinition}");
Console.WriteLine(" Is this a generic type? " _ & t.IsGenericType) Console.WriteLine(" Is this a generic type definition? " _ & t.IsGenericTypeDefinition)
GetGenericArguments メソッドを使用して、ジェネリック型引数を含む配列を取得します。
Type[] typeParameters = t.GetGenericArguments();
Dim typeParameters() As Type = t.GetGenericArguments()
型引数ごとに、 IsGenericParameter プロパティを使用して、型パラメーター (ジェネリック型定義など) か、型パラメーターに指定された型 (構築された型など) かを判断します。
Console.WriteLine($" List {typeParameters.Length} type arguments:"); foreach (Type tParam in typeParameters) { if (tParam.IsGenericParameter) { DisplayGenericParameter(tParam); } else { Console.WriteLine($" Type argument: {tParam}"); } }
Console.WriteLine(" List {0} type arguments:", _ typeParameters.Length) For Each tParam As Type In typeParameters If tParam.IsGenericParameter Then DisplayGenericParameter(tParam) Else Console.WriteLine(" Type argument: {0}", _ tParam) End If Next
型システムでは、ジェネリック型パラメーターは、通常の型と同様に、 Typeのインスタンスによって表されます。 次のコードは、ジェネリック型パラメーターを表す Type オブジェクトの名前とパラメーターの位置を表示します。 パラメーターの位置は、ここでの簡単な情報です。別のジェネリック型の型引数として使用されている型パラメーターを調べる場合は、より関心があります。
Console.WriteLine($" Type parameter: {tp.Name} position {tp.GenericParameterPosition}");
Private Shared Sub DisplayGenericParameter(ByVal tp As Type) Console.WriteLine(" Type parameter: {0} position {1}", _ tp.Name, tp.GenericParameterPosition)
GetGenericParameterConstraints メソッドを使用して単一の配列内のすべての制約を取得することにより、ジェネリック型パラメーターの基本型制約とインターフェイス制約を決定します。 制約は、特定の順序であるとは限りません。
foreach (Type iConstraint in tp.GetGenericParameterConstraints()) { if (iConstraint.IsInterface) { Console.WriteLine($" Interface constraint: {iConstraint}"); } } Console.WriteLine($" Base type constraint: {tp.BaseType ?? tp.BaseType: None}");
Dim classConstraint As Type = Nothing For Each iConstraint As Type In tp.GetGenericParameterConstraints() If iConstraint.IsInterface Then Console.WriteLine(" Interface constraint: {0}", _ iConstraint) End If Next If classConstraint IsNot Nothing Then Console.WriteLine(" Base type constraint: {0}", _ tp.BaseType) Else Console.WriteLine(" Base type constraint: None") End If
GenericParameterAttributes プロパティを使用して、型パラメーターに対する特殊な制約 (参照型である必要があるなど) を検出します。 プロパティには分散を表す値も含まれています。これは、次のコードに示すようにマスクできます。
GenericParameterAttributes sConstraints = tp.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
Dim sConstraints As GenericParameterAttributes = _ tp.GenericParameterAttributes And _ GenericParameterAttributes.SpecialConstraintMask
特殊制約属性はフラグであり、特別な制約を表さない同じフラグ (GenericParameterAttributes.None) も共変性または反変性を表しません。 したがって、これらの条件のいずれかをテストするには、適切なマスクを使用する必要があります。 この場合は、 GenericParameterAttributes.SpecialConstraintMask を使用して特殊な制約フラグを分離します。
if (sConstraints == GenericParameterAttributes.None) { Console.WriteLine(" No special constraints."); } else { if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.DefaultConstructorConstraint)) { Console.WriteLine(" Must have a parameterless constructor."); } if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.ReferenceTypeConstraint)) { Console.WriteLine(" Must be a reference type."); } if (GenericParameterAttributes.None != (sConstraints & GenericParameterAttributes.NotNullableValueTypeConstraint)) { Console.WriteLine(" Must be a non-nullable value type."); } }
If sConstraints = GenericParameterAttributes.None Then Console.WriteLine(" No special constraints.") Else If GenericParameterAttributes.None <> (sConstraints And _ GenericParameterAttributes.DefaultConstructorConstraint) Then Console.WriteLine(" Must have a parameterless constructor.") End If If GenericParameterAttributes.None <> (sConstraints And _ GenericParameterAttributes.ReferenceTypeConstraint) Then Console.WriteLine(" Must be a reference type.") End If If GenericParameterAttributes.None <> (sConstraints And _ GenericParameterAttributes.NotNullableValueTypeConstraint) Then Console.WriteLine(" Must be a non-nullable value type.") End If End If
ジェネリック型のインスタンスを構築する
ジェネリック型はテンプレートのようなものです。 ジェネリック型パラメーターに実際の型を指定しない限り、インスタンスを作成することはできません。 リフレクションを使用して実行時にこれを行うには、 MakeGenericType メソッドが必要です。
ジェネリック型を表す Type オブジェクトを取得します。 次のコードでは、2 つの異なる方法でジェネリック型Dictionary<TKey,TValue>を取得します。型を記述する文字列でType.GetType(String) メソッドのオーバーロードを使用し、構築された型GetGenericTypeDefinition (Visual Basic の
Dictionary\<String, Example>
) でDictionary(Of String, Example)
メソッドを呼び出します。 MakeGenericType メソッドにはジェネリック型の定義が必要です。// Use the typeof operator to create the generic type // definition directly. To specify the generic type definition, // omit the type arguments but retain the comma that separates // them. Type d1 = typeof(Dictionary<,>); // You can also obtain the generic type definition from a // constructed class. In this case, the constructed class // is a dictionary of Example objects, with String keys. Dictionary<string, Example> d2 = []; // Get a Type object that represents the constructed type, // and from that get the generic type definition. The // variables d1 and d4 contain the same type. Type d3 = d2.GetType(); Type d4 = d3.GetGenericTypeDefinition();
' Use the GetType operator to create the generic type ' definition directly. To specify the generic type definition, ' omit the type arguments but retain the comma that separates ' them. Dim d1 As Type = GetType(Dictionary(Of ,)) ' You can also obtain the generic type definition from a ' constructed class. In this case, the constructed class ' is a dictionary of Example objects, with String keys. Dim d2 As New Dictionary(Of String, Example) ' Get a Type object that represents the constructed type, ' and from that get the generic type definition. The ' variables d1 and d4 contain the same type. Dim d3 As Type = d2.GetType() Dim d4 As Type = d3.GetGenericTypeDefinition()
型パラメーターの代わりに型引数の配列を構築します。 配列には、型パラメーター リストに表示されるのと同じ順序で、正しい数の Type オブジェクトが含まれている必要があります。 この場合、キー (最初の型パラメーター) は String型であり、ディクショナリ内の値は
Example
という名前のクラスのインスタンスです。Type[] typeArgs = [typeof(string), typeof(Example)];
Dim typeArgs() As Type = _ {GetType(String), GetType(Example)}
MakeGenericType メソッドを呼び出して型引数を型パラメーターにバインドし、型を構築します。
Type constructed = d1.MakeGenericType(typeArgs);
Dim constructed As Type = _ d1.MakeGenericType(typeArgs)
CreateInstance(Type) メソッドのオーバーロードを使用して、構築された型のオブジェクトを作成します。 次のコードは、結果の
Example
オブジェクトにDictionary<String, Example>
クラスの 2 つのインスタンスを格納します。_ = Activator.CreateInstance(constructed);
Dim o As Object = Activator.CreateInstance(constructed)
例
次のコード例では、コードで使用されるジェネリック型の定義と構築された型を調べて、その情報を表示する DisplayGenericType
メソッドを定義します。
DisplayGenericType
メソッドは、IsGenericType、IsGenericParameter、およびGenericParameterPositionプロパティとGetGenericArguments メソッドの使用方法を示しています。
この例では、ジェネリック型パラメーターを調べて制約を表示する DisplayGenericParameter
メソッドも定義しています。
このコード例では、型パラメーターの制約を示すジェネリック型を含む一連のテスト型を定義し、これらの型に関する情報を表示する方法を示します。
この例では、型引数の配列を作成し、Dictionary<TKey,TValue> メソッドを呼び出すことによって、MakeGenericType クラスから型を構築します。 このプログラムでは、Typeを使用して構築されたMakeGenericType オブジェクトと、Type (Visual Basic のtypeof
) を使用して取得したGetType
オブジェクトを比較し、それらが同じであることを示します。 同様に、このプログラムでは、GetGenericTypeDefinition メソッドを使用して構築された型のジェネリック型定義を取得し、Type クラスを表すDictionary<TKey,TValue> オブジェクトと比較します。
using System.Reflection;
// Define an example interface.
public interface ITestArgument { }
// Define an example base class.
public class TestBase { }
// Define a generic class with one parameter. The parameter
// has three constraints: It must inherit TestBase, it must
// implement ITestArgument, and it must have a parameterless
// constructor.
public class Test<T> where T : TestBase, ITestArgument, new() { }
// Define a class that meets the constraints on the type
// parameter of class Test.
public class TestArgument : TestBase, ITestArgument
{
public TestArgument() { }
}
public class Example
{
// The following method displays information about a generic
// type.
private static void DisplayGenericType(Type t)
{
Console.WriteLine($"\r\n {t}");
Console.WriteLine($" Is this a generic type? {t.IsGenericType}");
Console.WriteLine($" Is this a generic type definition? {t.IsGenericTypeDefinition}");
// Get the generic type parameters or type arguments.
Type[] typeParameters = t.GetGenericArguments();
Console.WriteLine($" List {typeParameters.Length} type arguments:");
foreach (Type tParam in typeParameters)
{
if (tParam.IsGenericParameter)
{
DisplayGenericParameter(tParam);
}
else
{
Console.WriteLine($" Type argument: {tParam}");
}
}
}
// Displays information about a generic type parameter.
private static void DisplayGenericParameter(Type tp)
{
Console.WriteLine($" Type parameter: {tp.Name} position {tp.GenericParameterPosition}");
foreach (Type iConstraint in tp.GetGenericParameterConstraints())
{
if (iConstraint.IsInterface)
{
Console.WriteLine($" Interface constraint: {iConstraint}");
}
}
Console.WriteLine($" Base type constraint: {tp.BaseType ?? tp.BaseType: None}");
GenericParameterAttributes sConstraints =
tp.GenericParameterAttributes &
GenericParameterAttributes.SpecialConstraintMask;
if (sConstraints == GenericParameterAttributes.None)
{
Console.WriteLine(" No special constraints.");
}
else
{
if (GenericParameterAttributes.None != (sConstraints &
GenericParameterAttributes.DefaultConstructorConstraint))
{
Console.WriteLine(" Must have a parameterless constructor.");
}
if (GenericParameterAttributes.None != (sConstraints &
GenericParameterAttributes.ReferenceTypeConstraint))
{
Console.WriteLine(" Must be a reference type.");
}
if (GenericParameterAttributes.None != (sConstraints &
GenericParameterAttributes.NotNullableValueTypeConstraint))
{
Console.WriteLine(" Must be a non-nullable value type.");
}
}
}
public static void Main()
{
// Two ways to get a Type object that represents the generic
// type definition of the Dictionary class.
// Use the typeof operator to create the generic type
// definition directly. To specify the generic type definition,
// omit the type arguments but retain the comma that separates
// them.
Type d1 = typeof(Dictionary<,>);
// You can also obtain the generic type definition from a
// constructed class. In this case, the constructed class
// is a dictionary of Example objects, with String keys.
Dictionary<string, Example> d2 = [];
// Get a Type object that represents the constructed type,
// and from that get the generic type definition. The
// variables d1 and d4 contain the same type.
Type d3 = d2.GetType();
Type d4 = d3.GetGenericTypeDefinition();
// Display information for the generic type definition, and
// for the constructed type Dictionary<String, Example>.
DisplayGenericType(d1);
DisplayGenericType(d2.GetType());
// Construct an array of type arguments to substitute for
// the type parameters of the generic Dictionary class.
// The array must contain the correct number of types, in
// the same order that they appear in the type parameter
// list of Dictionary. The key (first type parameter)
// is of type string, and the type to be contained in the
// dictionary is Example.
Type[] typeArgs = [typeof(string), typeof(Example)];
// Construct the type Dictionary<String, Example>.
Type constructed = d1.MakeGenericType(typeArgs);
DisplayGenericType(constructed);
_ = Activator.CreateInstance(constructed);
Console.WriteLine("\r\nCompare types obtained by different methods:");
Console.WriteLine($" Are the constructed types equal? {d2.GetType() == constructed}");
Console.WriteLine($" Are the generic definitions equal? {d1 == constructed.GetGenericTypeDefinition()}");
// Demonstrate the DisplayGenericType and
// DisplayGenericParameter methods with the Test class
// defined above. This shows base, interface, and special
// constraints.
DisplayGenericType(typeof(Test<>));
}
}
Imports System.Reflection
Imports System.Collections.Generic
' Define an example interface.
Public Interface ITestArgument
End Interface
' Define an example base class.
Public Class TestBase
End Class
' Define a generic class with one parameter. The parameter
' has three constraints: It must inherit TestBase, it must
' implement ITestArgument, and it must have a parameterless
' constructor.
Public Class Test(Of T As {TestBase, ITestArgument, New})
End Class
' Define a class that meets the constraints on the type
' parameter of class Test.
Public Class TestArgument
Inherits TestBase
Implements ITestArgument
Public Sub New()
End Sub
End Class
Public Class Example
' The following method displays information about a generic
' type.
Private Shared Sub DisplayGenericType(ByVal t As Type)
Console.WriteLine(vbCrLf & t.ToString())
Console.WriteLine(" Is this a generic type? " _
& t.IsGenericType)
Console.WriteLine(" Is this a generic type definition? " _
& t.IsGenericTypeDefinition)
' Get the generic type parameters or type arguments.
Dim typeParameters() As Type = t.GetGenericArguments()
Console.WriteLine(" List {0} type arguments:", _
typeParameters.Length)
For Each tParam As Type In typeParameters
If tParam.IsGenericParameter Then
DisplayGenericParameter(tParam)
Else
Console.WriteLine(" Type argument: {0}", _
tParam)
End If
Next
End Sub
' The following method displays information about a generic
' type parameter. Generic type parameters are represented by
' instances of System.Type, just like ordinary types.
Private Shared Sub DisplayGenericParameter(ByVal tp As Type)
Console.WriteLine(" Type parameter: {0} position {1}", _
tp.Name, tp.GenericParameterPosition)
Dim classConstraint As Type = Nothing
For Each iConstraint As Type In tp.GetGenericParameterConstraints()
If iConstraint.IsInterface Then
Console.WriteLine(" Interface constraint: {0}", _
iConstraint)
End If
Next
If classConstraint IsNot Nothing Then
Console.WriteLine(" Base type constraint: {0}", _
tp.BaseType)
Else
Console.WriteLine(" Base type constraint: None")
End If
Dim sConstraints As GenericParameterAttributes = _
tp.GenericParameterAttributes And _
GenericParameterAttributes.SpecialConstraintMask
If sConstraints = GenericParameterAttributes.None Then
Console.WriteLine(" No special constraints.")
Else
If GenericParameterAttributes.None <> (sConstraints And _
GenericParameterAttributes.DefaultConstructorConstraint) Then
Console.WriteLine(" Must have a parameterless constructor.")
End If
If GenericParameterAttributes.None <> (sConstraints And _
GenericParameterAttributes.ReferenceTypeConstraint) Then
Console.WriteLine(" Must be a reference type.")
End If
If GenericParameterAttributes.None <> (sConstraints And _
GenericParameterAttributes.NotNullableValueTypeConstraint) Then
Console.WriteLine(" Must be a non-nullable value type.")
End If
End If
End Sub
Public Shared Sub Main()
' Two ways to get a Type object that represents the generic
' type definition of the Dictionary class.
'
' Use the GetType operator to create the generic type
' definition directly. To specify the generic type definition,
' omit the type arguments but retain the comma that separates
' them.
Dim d1 As Type = GetType(Dictionary(Of ,))
' You can also obtain the generic type definition from a
' constructed class. In this case, the constructed class
' is a dictionary of Example objects, with String keys.
Dim d2 As New Dictionary(Of String, Example)
' Get a Type object that represents the constructed type,
' and from that get the generic type definition. The
' variables d1 and d4 contain the same type.
Dim d3 As Type = d2.GetType()
Dim d4 As Type = d3.GetGenericTypeDefinition()
' Display information for the generic type definition, and
' for the constructed type Dictionary(Of String, Example).
DisplayGenericType(d1)
DisplayGenericType(d2.GetType())
' Construct an array of type arguments to substitute for
' the type parameters of the generic Dictionary class.
' The array must contain the correct number of types, in
' the same order that they appear in the type parameter
' list of Dictionary. The key (first type parameter)
' is of type string, and the type to be contained in the
' dictionary is Example.
Dim typeArgs() As Type = _
{GetType(String), GetType(Example)}
' Construct the type Dictionary(Of String, Example).
Dim constructed As Type = _
d1.MakeGenericType(typeArgs)
DisplayGenericType(constructed)
Dim o As Object = Activator.CreateInstance(constructed)
Console.WriteLine(vbCrLf & _
"Compare types obtained by different methods:")
Console.WriteLine(" Are the constructed types equal? " _
& (d2.GetType() Is constructed))
Console.WriteLine(" Are the generic definitions equal? " _
& (d1 Is constructed.GetGenericTypeDefinition()))
' Demonstrate the DisplayGenericType and
' DisplayGenericParameter methods with the Test class
' defined above. This shows base, interface, and special
' constraints.
DisplayGenericType(GetType(Test(Of )))
End Sub
End Class
こちらも参照ください
.NET