GenericTypeParameterBuilderとは? わかりやすく解説

GenericTypeParameterBuilder クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

動的に定義されたジェネリック型およびジェネリック メソッドのジェネリック型パラメータを定義および作成します。このクラスは継承できません。

名前空間: System.Reflection.Emit
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<ComVisibleAttribute(True)> _
Public NotInheritable Class
 GenericTypeParameterBuilder
    Inherits Type
Dim instance As GenericTypeParameterBuilder
[ComVisibleAttribute(true)] 
public sealed class GenericTypeParameterBuilder
 : Type
[ComVisibleAttribute(true)] 
public ref class GenericTypeParameterBuilder
 sealed : public Type
/** @attribute ComVisibleAttribute(true) */ 
public final class GenericTypeParameterBuilder
 extends Type
ComVisibleAttribute(true) 
public final class GenericTypeParameterBuilder
 extends Type
解説解説

TypeBuilder.DefineGenericParameters メソッドを使用して、型パラメータを動的型に追加し、動的型をジェネリック型にするか、MethodBuilder.DefineGenericParameters メソッドを使用して、型パラメータを動的メソッドに追加することによって、GenericTypeParameterBuilder オブジェクトの配列を取得できます。GenericTypeParameterBuilder オブジェクトを使用して、型パラメータに制約を加えます。次の 3 種類の制約があります。

インターフェイスの制約と特殊な制約は、GenericTypeParameterBuilder クラスのメソッドを使用して取得することはできません。型パラメータを含むジェネリック型を作成すると、その Type オブジェクトを使用して制約をリフレクションできるようになります。Type.GetGenericArguments メソッドを使用して型パラメータを取得し、各型パラメータに対して、Type.GetGenericParameterConstraints メソッドを使用して基本型の制約とインターフェイスの制約を取得します。特殊な制約を取得するには、Type.GenericParameterAttributes プロパティを使用します。

使用例使用例

2 つの型パラメータを持つジェネリック型を作成し、アセンブリ GenericEmitExample1.dll に保存するコード例を次に示します。MSIL 逆アセンブラ (Ildasm.exe) を使用して、生成された型を表示できます。動的ジェネリック型の定義に関連する手順の詳細については、「方法 : リフレクション出力を使用してジェネリック型を定義する」を参照してください。

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Collections.Generic

' Define a trivial base class and two trivial interfaces 
' to use when demonstrating constraints.
'
Public Class ExampleBase
End Class

Public Interface IExampleA
End Interface

Public Interface IExampleB
End Interface

' Define a trivial type that can substitute for type parameter 
' TSecond.
'
Public Class ExampleDerived
    Inherits ExampleBase
    Implements IExampleA, IExampleB
End Class

Public Class Example
    Public Shared Sub Main()
        ' Define a dynamic assembly to contain the sample type. The
        ' assembly will not be run, but only saved to disk, so
        ' AssemblyBuilderAccess.Save is specified.
        '
        Dim myDomain As AppDomain = AppDomain.CurrentDomain
        Dim myAsmName As New
 AssemblyName("GenericEmitExample1")
        Dim myAssembly As AssemblyBuilder =
 myDomain.DefineDynamicAssembly( _
            myAsmName, _
            AssemblyBuilderAccess.RunAndSave)

        ' An assembly is made up of executable modules. For a single-
        ' module assembly, the module name and file name are the same
 
        ' as the assembly name. 
        '
        Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule(
 _
            myAsmName.Name, _
            myAsmName.Name & ".dll")

        ' Get type objects for the base class trivial interfaces to
        ' be used as constraints.
        '
        Dim baseType As Type = GetType(ExampleBase)
        Dim interfaceA As Type = GetType(IExampleA)
        Dim interfaceB As Type = GetType(IExampleB)
                
        ' Define the sample type.
        '
        Dim myType As TypeBuilder = myModule.DefineType(
 _
            "Sample", _
            TypeAttributes.Public)

        Console.WriteLine("Type 'Sample' is generic: {0}",
 _
            myType.IsGenericType)

        ' Define type parameters for the type. Until you do this, 
        ' the type is not generic, as the preceding and following 
        ' WriteLine statements show. The type parameter names are
        ' specified as an array of strings. To make the code
        ' easier to read, each GenericTypeParameterBuilder is placed
        ' in a variable with the same name as the type parameter.
        ' 
        Dim typeParamNames() As String
 = {"TFirst", "TSecond"}
        Dim typeParams() As GenericTypeParameterBuilder
 = _
            myType.DefineGenericParameters(typeParamNames)

        Dim TFirst As GenericTypeParameterBuilder
 = typeParams(0)
        Dim TSecond As GenericTypeParameterBuilder
 = typeParams(1)

        Console.WriteLine("Type 'Sample' is generic: {0}",
 _
            myType.IsGenericType)

        ' Apply constraints to the type parameters.
        '
        ' A type that is substituted for the first parameter, TFirst
,
        ' must be a reference type and must have a parameterless
        ' constructor.
        TFirst.SetGenericParameterAttributes( _
            GenericParameterAttributes.DefaultConstructorConstraint _
            Or GenericParameterAttributes.ReferenceTypeConstraint)

        ' A type that is substituted for the second type
        ' parameter must implement IExampleA and IExampleB, and
        ' inherit from the trivial test class ExampleBase. The
        ' interface constraints are specified as an array 
        ' containing the interface types.
        TSecond.SetBaseTypeConstraint(baseType)
        Dim interfaceTypes() As Type = {interfaceA,
 interfaceB}
        TSecond.SetInterfaceConstraints(interfaceTypes)

        ' The following code adds a private field named ExampleField
,
        ' of type TFirst.
        Dim exField As FieldBuilder = _
            myType.DefineField("ExampleField", TFirst,
 _
                FieldAttributes.Private)

        ' Define a Shared method that takes an array of TFirst and 
        ' returns a List(Of TFirst) containing all the elements of 
        ' the array. To define this method it is necessary to create
        ' the type List(Of TFirst) by calling MakeGenericType on the
        ' generic type definition, List(Of T). (The T is omitted with
        ' the GetType operator when you get the generic type 
        ' definition.) The parameter type is created by using the
        ' MakeArrayType method. 
        '
        Dim listOf As Type = GetType(List(Of
 ))
        Dim listOfTFirst As Type = listOf.MakeGenericType(TFirst)
        Dim mParamTypes() As Type = { TFirst.MakeArrayType()
 }

        Dim exMethod As MethodBuilder = _
            myType.DefineMethod("ExampleMethod", _
                MethodAttributes.Public Or MethodAttributes.Static,
 _
                listOfTFirst, _
                mParamTypes)

        ' Emit the method body. 
        ' The method body consists of just three opcodes, to load 
        ' the input array onto the execution stack, to call the 
        ' List(Of TFirst) constructor that takes IEnumerable(Of TFirst)
,
        ' which does all the work of putting the input elements into
        ' the list, and to return, leaving the list on the stack. The
        ' hard work is getting the constructor.
        ' 
        ' The GetConstructor method is not supported on a 
        ' GenericTypeParameterBuilder, so it is not possible to get
 
        ' the constructor of List(Of TFirst) directly. There are two
        ' steps, first getting the constructor of List(Of T) and then
        ' calling a method that converts it to the corresponding 
        ' constructor of List(Of TFirst).
        '
        ' The constructor needed here is the one that takes an
        ' IEnumerable(Of T). Note, however, that this is not the 
        ' generic type definition of IEnumerable(Of T); instead, the
        ' T from List(Of T) must be substituted for the T of 
        ' IEnumerable(Of T). (This seems confusing only because both
        ' types have type parameters named T. That is why this example
        ' uses the somewhat silly names TFirst and TSecond.) To get
        ' the type of the constructor argument, take the generic
        ' type definition IEnumerable(Of T) (expressed as 
        ' IEnumerable(Of ) when you use the GetType operator) and 
        ' call MakeGenericType with the first generic type parameter
        ' of List(Of T). The constructor argument list must be passed
        ' as an array, with just one argument in this case.
        ' 
        ' Now it is possible to get the constructor of List(Of T),
        ' using GetConstructor on the generic type definition. To get
        ' the constructor of List(Of TFirst), pass List(Of TFirst) and
        ' the constructor from List(Of T) to the static
        ' TypeBuilder.GetConstructor method.
        '
        Dim ilgen As ILGenerator = exMethod.GetILGenerator()
        
        Dim ienumOf As Type = GetType(IEnumerable(Of
 ))
        Dim listOfTParams() As Type = listOf.GetGenericArguments()
        Dim TfromListOf As Type = listOfTParams(0)
        Dim ienumOfT As Type = ienumOf.MakeGenericType(TfromListOf)
        Dim ctorArgs() As Type = { ienumOfT
 }

        Dim ctorPrep As ConstructorInfo = _
            listOf.GetConstructor(ctorArgs)
        Dim ctor As ConstructorInfo = _
            TypeBuilder.GetConstructor(listOfTFirst, ctorPrep)

        ilgen.Emit(OpCodes.Ldarg_0)
        ilgen.Emit(OpCodes.Newobj, ctor)
        ilgen.Emit(OpCodes.Ret)

        ' Create the type and save the assembly. 
        Dim finished As Type = myType.CreateType()
        myAssembly.Save(myAsmName.Name & ".dll")

        ' Invoke the method.
        ' ExampleMethod is not generic, but the type it belongs to is
        ' generic, so in order to get a MethodInfo that can be invoked
        ' it is necessary to create a constructed type. The Example
 
        ' class satisfies the constraints on TFirst, because it is a
 
        ' reference type and has a default constructor. In order to
        ' have a class that satisfies the constraints on TSecond, 
        ' this code example defines the ExampleDerived type. These
        ' two types are passed to MakeGenericMethod to create the
        ' constructed type.
        '
        Dim typeArgs() As Type = _
            { GetType(Example), GetType(ExampleDerived)
 }
        Dim constructed As Type = finished.MakeGenericType(typeArgs)
        Dim mi As MethodInfo = constructed.GetMethod("ExampleMethod")

        ' Create an array of Example objects, as input to the generic
        ' method. This array must be passed as the only element of an
 
        ' array of arguments. The first argument of Invoke is 
        ' Nothing, because ExampleMethod is Shared. Display the count
        ' on the resulting List(Of Example).
        ' 
        Dim input() As Example = { New
 Example(), New Example() }
        Dim arguments() As Object
 = { input }

        Dim listX As List(Of
 Example) = mi.Invoke(Nothing, arguments)

        Console.WriteLine(vbLf & _
            "There are {0} elements in the List(Of Example).",
 _
            listX.Count _ 
        )

        DisplayGenericParameters(finished)
    End Sub

    Private Shared Sub DisplayGenericParameters(ByVal
 t As Type)

        If Not t.IsGenericType Then
            Console.WriteLine("Type '{0}' is not generic.")
            Return
        End If
        If Not t.IsGenericTypeDefinition Then
 _
            t = t.GetGenericTypeDefinition()

        Dim typeParameters() As Type = t.GetGenericArguments()
        Console.WriteLine(vbCrLf & _
            "Listing {0} type parameters for type '{1}'.",
 _
            typeParameters.Length, t)

        For Each tParam As
 Type In typeParameters

            Console.WriteLine(vbCrLf & "Type parameter {0}:",
 _
                tParam.ToString())

            For Each c As
 Type In tParam.GetGenericParameterConstraints()
                If c.IsInterface Then
                    Console.WriteLine("    Interface constraint:
 {0}", c)
                Else
                    Console.WriteLine("    Base type constraint:
 {0}", c)
                End If
            Next 

            ListConstraintAttributes(tParam)
        Next tParam
    End Sub

    ' List the constraint flags. The GenericParameterAttributes
    ' enumeration contains two sets of attributes, variance and
    ' constraints. For this example, only constraints are used.
    '
    Private Shared Sub ListConstraintAttributes(ByVal
 t As Type)

        ' Mask off the constraint flags. 
        Dim constraints As GenericParameterAttributes
 = _
            t.GenericParameterAttributes And _
            GenericParameterAttributes.SpecialConstraintMask

        If (constraints And GenericParameterAttributes.ReferenceTypeConstraint)
 _
                <> GenericParameterAttributes.None Then
 _
            Console.WriteLine("    ReferenceTypeConstraint")

        If (constraints And GenericParameterAttributes.NotNullableValueTypeConstraint)
 _
                <> GenericParameterAttributes.None Then
 _
            Console.WriteLine("    NotNullableValueTypeConstraint")

        If (constraints And GenericParameterAttributes.DefaultConstructorConstraint)
 _
                <> GenericParameterAttributes.None Then
 _
            Console.WriteLine("    DefaultConstructorConstraint")

    End Sub 

End Class

' This code example produces the following output:
'
'Type 'Sample' is generic: False
'Type 'Sample' is generic: True
'
'There are 2 elements in the List(Of Example).
'
'Listing 2 type parameters for type 'Sample[TFirst,TSecond]'.
'
'Type parameter TFirst:
'    ReferenceTypeConstraint
'    DefaultConstructorConstraint
'
'Type parameter TSecond:
'    Interface constraint: IExampleA
'    Interface constraint: IExampleB
'    Base type constraint: ExampleBase
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;

// Define a trivial base class and two trivial interfaces 
// to use when demonstrating constraints.
//
public class ExampleBase {}

public interface IExampleA {}

public interface IExampleB {}

// Define a trivial type that can substitute for type parameter 
// TSecond.
//
public class ExampleDerived : ExampleBase,
 IExampleA, IExampleB {}


public class Example
{
    public static void Main()
    {
        // Define a dynamic assembly to contain the sample type. The
        // assembly will not be run, but only saved to disk, so
        // AssemblyBuilderAccess.Save is specified.
        //
        AppDomain myDomain = AppDomain.CurrentDomain;
        AssemblyName myAsmName = new AssemblyName("GenericEmitExample1");
        AssemblyBuilder myAssembly = 
            myDomain.DefineDynamicAssembly(myAsmName, 
                AssemblyBuilderAccess.RunAndSave);

        // An assembly is made up of executable modules. For a single-
        // module assembly, the module name and file name are the same
 
        // as the assembly name. 
        //
        ModuleBuilder myModule = 
            myAssembly.DefineDynamicModule(myAsmName.Name, 
               myAsmName.Name + ".dll");

        // Get type objects for the base class trivial interfaces to
        // be used as constraints.
        //
        Type baseType = typeof(ExampleBase);
        Type interfaceA = typeof(IExampleA);
        Type interfaceB = typeof(IExampleB);
                
        // Define the sample type.
        //
        TypeBuilder myType = 
            myModule.DefineType("Sample", TypeAttributes.Public);

        Console.WriteLine("Type 'Sample' is generic: {0}", 
            myType.IsGenericType);

        // Define type parameters for the type. Until you do this, 
        // the type is not generic, as the preceding and following 
        // WriteLine statements show. The type parameter names are
        // specified as an array of strings. To make the code
        // easier to read, each GenericTypeParameterBuilder is placed
        // in a variable with the same name as the type parameter.
        // 
        string[] typeParamNames = {"TFirst", "TSecond"};
        GenericTypeParameterBuilder[] typeParams = 
            myType.DefineGenericParameters(typeParamNames);

        GenericTypeParameterBuilder TFirst = typeParams[0];
        GenericTypeParameterBuilder TSecond = typeParams[1];

        Console.WriteLine("Type 'Sample' is generic: {0}", 
            myType.IsGenericType);

        // Apply constraints to the type parameters.
        //
        // A type that is substituted for the first parameter, TFirst
,
        // must be a reference type and must have a parameterless
        // constructor.
        TFirst.SetGenericParameterAttributes(
            GenericParameterAttributes.DefaultConstructorConstraint |
            GenericParameterAttributes.ReferenceTypeConstraint);

        // A type that is substituted for the second type
        // parameter must implement IExampleA and IExampleB, and
        // inherit from the trivial test class ExampleBase. The
        // interface constraints are specified as an array 
        // containing the interface types.
        TSecond.SetBaseTypeConstraint(baseType);
        Type[] interfaceTypes = {interfaceA, interfaceB};
        TSecond.SetInterfaceConstraints(interfaceTypes);

        // The following code adds a private field named ExampleField
,
        // of type TFirst.
        FieldBuilder exField = 
            myType.DefineField("ExampleField", TFirst, 
                FieldAttributes.Private);

        // Define a static method that takes an array of TFirst and
 
        // returns a List<TFirst> containing all the elements
 of 
        // the array. To define this method it is necessary to create
        // the type List<TFirst> by calling MakeGenericType on
 the
        // generic type definition, List<T>. (The T is omitted
 with
        // the typeof operator when you get the generic type 
        // definition.) The parameter type is created by using the
        // MakeArrayType method. 
        //
        Type listOf = typeof(List<>);
        Type listOfTFirst = listOf.MakeGenericType(TFirst);
        Type[] mParamTypes = {TFirst.MakeArrayType()};

        MethodBuilder exMethod = 
            myType.DefineMethod("ExampleMethod", 
                MethodAttributes.Public | MethodAttributes.Static, 
                listOfTFirst, 
                mParamTypes);

        // Emit the method body. 
        // The method body consists of just three opcodes, to load 
        // the input array onto the execution stack, to call the 
        // List<TFirst> constructor that takes IEnumerable<TFirst>
,
        // which does all the work of putting the input elements into
        // the list, and to return, leaving the list on the stack. The
        // hard work is getting the constructor.
        // 
        // The GetConstructor method is not supported on a 
        // GenericTypeParameterBuilder, so it is not possible to get
 
        // the constructor of List<TFirst> directly. There are
 two
        // steps, first getting the constructor of List<T> and
 then
        // calling a method that converts it to the corresponding 
        // constructor of List<TFirst>.
        //
        // The constructor needed here is the one that takes an
        // IEnumerable<T>. Note, however, that this is not the
 
        // generic type definition of IEnumerable<T>; instead,
 the
        // T from List<T> must be substituted for the T of 
        // IEnumerable<T>. (This seems confusing only because
 both
        // types have type parameters named T. That is why this example
        // uses the somewhat silly names TFirst and TSecond.) To get
        // the type of the constructor argument, take the generic
        // type definition IEnumerable<T> (expressed as 
        // IEnumerable<> when you use the typeof operator) and
 
        // call MakeGenericType with the first generic type parameter
        // of List<T>. The constructor argument list must be passed
        // as an array, with just one argument in this case.
        // 
        // Now it is possible to get the constructor of List<T>
,
        // using GetConstructor on the generic type definition. To get
        // the constructor of List<TFirst>, pass List<TFirst>
 and
        // the constructor from List<T> to the static
        // TypeBuilder.GetConstructor method.
        //
        ILGenerator ilgen = exMethod.GetILGenerator();
        
        Type ienumOf = typeof(IEnumerable<>);
        Type TfromListOf = listOf.GetGenericArguments()[0];
        Type ienumOfT = ienumOf.MakeGenericType(TfromListOf);
        Type[] ctorArgs = {ienumOfT};

        ConstructorInfo ctorPrep = listOf.GetConstructor(ctorArgs);
        ConstructorInfo ctor = 
            TypeBuilder.GetConstructor(listOfTFirst, ctorPrep);

        ilgen.Emit(OpCodes.Ldarg_0);
        ilgen.Emit(OpCodes.Newobj, ctor);
        ilgen.Emit(OpCodes.Ret);

        // Create the type and save the assembly. 
        Type finished = myType.CreateType();
        myAssembly.Save(myAsmName.Name+".dll");

        // Invoke the method.
        // ExampleMethod is not generic, but the type it belongs to
 is
        // generic, so in order to get a MethodInfo that can be invoked
        // it is necessary to create a constructed type. The Example
 
        // class satisfies the constraints on TFirst, because it is
 a 
        // reference type and has a default constructor. In order to
        // have a class that satisfies the constraints on TSecond, 
        // this code example defines the ExampleDerived type. These
        // two types are passed to MakeGenericMethod to create the
        // constructed type.
        //
        Type[] typeArgs = {typeof(Example), typeof(ExampleDerived)};
        Type constructed = finished.MakeGenericType(typeArgs);
        MethodInfo mi = constructed.GetMethod("ExampleMethod");

        // Create an array of Example objects, as input to the generic
        // method. This array must be passed as the only element of
 an 
        // array of arguments. The first argument of Invoke is 
        // null, because ExampleMethod is static. Display the count
        // on the resulting List<Example>.
        // 
        Example[] input = {new Example(), new
 Example()};
        object[] arguments = {input};

        List<Example> listX = 
            (List<Example>) mi.Invoke(null, arguments);

        Console.WriteLine(
            "\nThere are {0} elements in the List<Example>.",
 
            listX.Count);

        DisplayGenericParameters(finished);
    }

    private static void
 DisplayGenericParameters(Type t)
    {
        if (!t.IsGenericType)
        {
            Console.WriteLine("Type '{0}' is not generic.");
            return;
        }
        if (!t.IsGenericTypeDefinition) 
        {
            t = t.GetGenericTypeDefinition();
        }

        Type[] typeParameters = t.GetGenericArguments();
        Console.WriteLine("\nListing {0} type parameters for
 type '{1}'.",
            typeParameters.Length, t);

        foreach( Type tParam in typeParameters
 )
        {
            Console.WriteLine("\r\nType parameter {0}:", tParam.ToString());

            foreach( Type c in tParam.GetGenericParameterConstraints()
 )
            {
                if (c.IsInterface)
                {
                    Console.WriteLine("    Interface constraint: {0}",
 c);
                }
                else
                {
                    Console.WriteLine("    Base type constraint: {0}",
 c);
                }
            }

            ListConstraintAttributes(tParam);
        }
    }

    // List the constraint flags. The GenericParameterAttributes
    // enumeration contains two sets of attributes, variance and
    // constraints. For this example, only constraints are used.
    //
    private static void
 ListConstraintAttributes(Type t)
    {
        // Mask off the constraint flags. 
        GenericParameterAttributes constraints = 
            t.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;

        if ((constraints & GenericParameterAttributes.ReferenceTypeConstraint)
            != GenericParameterAttributes.None) 
        {
            Console.WriteLine("    ReferenceTypeConstraint");
        }

        if ((constraints & GenericParameterAttributes.NotNullableValueTypeConstraint)
            != GenericParameterAttributes.None) 
        {
            Console.WriteLine("    NotNullableValueTypeConstraint");
        }

        if ((constraints & GenericParameterAttributes.DefaultConstructorConstraint)
            !=GenericParameterAttributes.None) 
        {
            Console.WriteLine("    DefaultConstructorConstraint");
        }
    }
}

/* This code example produces the following output:

Type 'Sample' is generic: False
Type 'Sample' is generic: True

There are 2 elements in the List<Example>.

Listing 2 type parameters for type 'Sample[TFirst,TSecond]'.

Type parameter TFirst:
    ReferenceTypeConstraint
    DefaultConstructorConstraint

Type parameter TSecond:
    Interface constraint: IExampleA
    Interface constraint: IExampleB
    Base type constraint: ExampleBase
 */
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Collections::Generic;

// Dummy class to satisfy TFirst constraints.
//
public ref class Example {};

// Define a trivial base class and two trivial interfaces 
// to use when demonstrating constraints.
//
public ref class ExampleBase {};
public interface class IExampleA {};
public interface class IExampleB {};

// Define a trivial type that can substitute for type parameter 
// TSecond.
//
public ref class ExampleDerived : ExampleBase,
 IExampleA, IExampleB {};

// List the constraint flags. The GenericParameterAttributes
// enumeration contains two sets of attributes, variance and
// constraints. For this example, only constraints are used.
//
static void ListConstraintAttributes( Type^
 t )
{
   // Mask off the constraint flags. 
   GenericParameterAttributes constraints = 
       t->GenericParameterAttributes & 
       GenericParameterAttributes::SpecialConstraintMask;

   if ((constraints & GenericParameterAttributes::ReferenceTypeConstraint)
           != GenericParameterAttributes::None)
       Console::WriteLine( L"    ReferenceTypeConstraint");

   if ((constraints & GenericParameterAttributes::NotNullableValueTypeConstraint)
           != GenericParameterAttributes::None)
       Console::WriteLine( L"    NotNullableValueTypeConstraint");

   if ((constraints & GenericParameterAttributes::DefaultConstructorConstraint)
           != GenericParameterAttributes::None)
       Console::WriteLine( L"    DefaultConstructorConstraint");
}

static void DisplayGenericParameters( Type^
 t )
{
   if (!t->IsGenericType)
   {
       Console::WriteLine( L"Type '{0}' is not generic." );
       return;
   }
   if (!t->IsGenericTypeDefinition)
       t = t->GetGenericTypeDefinition();

   array<Type^>^ typeParameters = t->GetGenericArguments();
   Console::WriteLine( L"\r\nListing {0} type parameters for
 type '{1}'.", 
       typeParameters->Length, t );

   for each ( Type^ tParam in typeParameters
 )
   {
       Console::WriteLine( L"\r\nType parameter {0}:", 
           tParam->ToString() );

       for each (Type^ c in tParam->GetGenericParameterConstraints())
       {
           if (c->IsInterface)
               Console::WriteLine( L"    Interface constraint: {0}", c);
           else
               Console::WriteLine( L"    Base type constraint: {0}", c);
       }
       ListConstraintAttributes(tParam);
   }
}

void main()
{
   // Define a dynamic assembly to contain the sample type. The
   // assembly will be run and also saved to disk, so
   // AssemblyBuilderAccess.RunAndSave is specified.
   //
   AppDomain^ myDomain = AppDomain::CurrentDomain;
   AssemblyName^ myAsmName = gcnew AssemblyName( L"GenericEmitExample1"
 );
   AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly( 
       myAsmName, AssemblyBuilderAccess::RunAndSave );

   // An assembly is made up of executable modules. For a single-
   // module assembly, the module name and file name are the same 
   // as the assembly name. 
   //
   ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( 
       myAsmName->Name, String::Concat( myAsmName->Name, L".dll"
 ) );

   // Get type objects for the base class trivial interfaces to
   // be used as constraints.
   //
   Type^ baseType = ExampleBase::typeid; 
   Type^ interfaceA = IExampleA::typeid; 
   Type^ interfaceB = IExampleB::typeid;
   
   // Define the sample type.
   //
   TypeBuilder^ myType = myModule->DefineType( L"Sample", 
       TypeAttributes::Public );
   
   Console::WriteLine( L"Type 'Sample' is generic: {0}", 
       myType->IsGenericType );
   
   // Define type parameters for the type. Until you do this, 
   // the type is not generic, as the preceding and following 
   // WriteLine statements show. The type parameter names are
   // specified as an array of strings. To make the code
   // easier to read, each GenericTypeParameterBuilder is placed
   // in a variable with the same name as the type parameter.
   // 
   array<String^>^typeParamNames = {L"TFirst",L"TSecond"};
   array<GenericTypeParameterBuilder^>^typeParams = 
       myType->DefineGenericParameters( typeParamNames );

   GenericTypeParameterBuilder^ TFirst = typeParams[0];
   GenericTypeParameterBuilder^ TSecond = typeParams[1];

   Console::WriteLine( L"Type 'Sample' is generic: {0}", 
       myType->IsGenericType );
   
   // Apply constraints to the type parameters.
   //
   // A type that is substituted for the first parameter, TFirst,
   // must be a reference type and must have a parameterless
   // constructor.
   TFirst->SetGenericParameterAttributes( 
       GenericParameterAttributes::DefaultConstructorConstraint | 
       GenericParameterAttributes::ReferenceTypeConstraint 
   );

   // A type that is substituted for the second type
   // parameter must implement IExampleA and IExampleB, and
   // inherit from the trivial test class ExampleBase. The
   // interface constraints are specified as an array
   // containing the interface types. 
   array<Type^>^interfaceTypes = { interfaceA, interfaceB };
   TSecond->SetInterfaceConstraints( interfaceTypes );
   TSecond->SetBaseTypeConstraint( baseType );

   // The following code adds a private field named ExampleField,
   // of type TFirst.
   FieldBuilder^ exField = 
       myType->DefineField("ExampleField", TFirst, 
           FieldAttributes::Private);

   // Define a static method that takes an array of TFirst and 
   // returns a List<TFirst> containing all the elements of 
   // the array. To define this method it is necessary to create
   // the type List<TFirst> by calling MakeGenericType on the
   // generic type definition, generic<T> List. 
   // The parameter type is created by using the
   // MakeArrayType method. 
   //
   Type^ listOf = List::typeid;
   Type^ listOfTFirst = listOf->MakeGenericType(TFirst);
   array<Type^>^ mParamTypes = { TFirst->MakeArrayType() };

   MethodBuilder^ exMethod = 
       myType->DefineMethod("ExampleMethod", 
           MethodAttributes::Public | MethodAttributes::Static, 
           listOfTFirst, 
           mParamTypes);

   // Emit the method body. 
   // The method body consists of just three opcodes, to load 
   // the input array onto the execution stack, to call the 
   // List<TFirst> constructor that takes IEnumerable<TFirst>
,
   // which does all the work of putting the input elements into
   // the list, and to return, leaving the list on the stack. The
   // hard work is getting the constructor.
   // 
   // The GetConstructor method is not supported on a 
   // GenericTypeParameterBuilder, so it is not possible to get 
   // the constructor of List<TFirst> directly. There are two
   // steps, first getting the constructor of generic<T> List
 and then
   // calling a method that converts it to the corresponding 
   // constructor of List<TFirst>.
   //
   // The constructor needed here is the one that takes an
   // IEnumerable<T>. Note, however, that this is not the 
   // generic type definition of generic<T> IEnumerable; instead,
 the
   // T from generic<T> List must be substituted for the T of
 
   // generic<T> IEnumerable. (This seems confusing only because
 both
   // types have type parameters named T. That is why this example
   // uses the somewhat silly names TFirst and TSecond.) To get
   // the type of the constructor argument, take the generic
   // type definition generic<T> IEnumerable and 
   // call MakeGenericType with the first generic type parameter
   // of generic<T> List. The constructor argument list must be
 passed
   // as an array, with just one argument in this case.
   // 
   // Now it is possible to get the constructor of generic<T>
 List,
   // using GetConstructor on the generic type definition. To get
   // the constructor of List<TFirst>, pass List<TFirst>
 and
   // the constructor from generic<T> List to the static
   // TypeBuilder.GetConstructor method.
   //
   ILGenerator^ ilgen = exMethod->GetILGenerator();
        
   Type^ ienumOf = IEnumerable::typeid;
   Type^ TfromListOf = listOf->GetGenericArguments()[0];
   Type^ ienumOfT = ienumOf->MakeGenericType(TfromListOf);
   array<Type^>^ ctorArgs = {ienumOfT};

   ConstructorInfo^ ctorPrep = listOf->GetConstructor(ctorArgs);
   ConstructorInfo^ ctor = 
       TypeBuilder::GetConstructor(listOfTFirst, ctorPrep);

   ilgen->Emit(OpCodes::Ldarg_0);
   ilgen->Emit(OpCodes::Newobj, ctor);
   ilgen->Emit(OpCodes::Ret);

   // Create the type and save the assembly. 
   Type^ finished = myType->CreateType();
   myAssembly->Save( String::Concat( myAsmName->Name, L".dll" ) );

   // Invoke the method.
   // ExampleMethod is not generic, but the type it belongs to is
   // generic, so in order to get a MethodInfo that can be invoked
   // it is necessary to create a constructed type. The Example 
   // class satisfies the constraints on TFirst, because it is a 
   // reference type and has a default constructor. In order to
   // have a class that satisfies the constraints on TSecond, 
   // this code example defines the ExampleDerived type. These
   // two types are passed to MakeGenericMethod to create the
   // constructed type.
   //
   array<Type^>^ typeArgs = 
       { Example::typeid, ExampleDerived::typeid };
   Type^ constructed = finished->MakeGenericType(typeArgs);
   MethodInfo^ mi = constructed->GetMethod("ExampleMethod");

   // Create an array of Example objects, as input to the generic
   // method. This array must be passed as the only element of an 
   // array of arguments. The first argument of Invoke is 
   // null, because ExampleMethod is static. Display the count
   // on the resulting List<Example>.
   // 
   array<Example^>^ input = { gcnew Example(), gcnew Example() };
   array<Object^>^ arguments = { input };

   List<Example^>^ listX = 
       (List<Example^>^) mi->Invoke(nullptr, arguments);

   Console::WriteLine(
       "\nThere are {0} elements in the List<Example>.",
 
       listX->Count);

   DisplayGenericParameters(finished);
}

/* This code example produces the following output:

Type 'Sample' is generic: False
Type 'Sample' is generic: True

There are 2 elements in the List<Example>.

Listing 2 type parameters for type 'Sample[TFirst,TSecond]'.

Type parameter TFirst:
    ReferenceTypeConstraint
    DefaultConstructorConstraint

Type parameter TSecond:
    Interface constraint: IExampleA
    Interface constraint: IExampleB
    Base type constraint: ExampleBase
 */
継承階層継承階層
System.Object
   System.Reflection.MemberInfo
     System.Type
      System.Reflection.Emit.GenericTypeParameterBuilder
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
GenericTypeParameterBuilder メンバ
System.Reflection.Emit 名前空間
TypeBuilder.DefineGenericParameters
その他の技術情報
方法 : リフレクション出力を使用してジェネリック型を定義する

GenericTypeParameterBuilder プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Assembly オーバーライドされます。 現在の型パラメータが属するジェネリック型の定義を格納する動的アセンブリを表す Assembly オブジェクトを取得します。
パブリック プロパティ AssemblyQualifiedName オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を取得します。
パブリック プロパティ Attributes  Type に関連付けられている属性を取得します。 ( Type から継承されます。)
パブリック プロパティ BaseType オーバーライドされます。 現在のジェネリック型パラメータの基本型の制約を取得します。
パブリック プロパティ ContainsGenericParameters オーバーライドされます。 常に false を取得します。
パブリック プロパティ DeclaringMethod オーバーライドされます。 現在の GenericTypeParameterBuilder がジェネリック メソッドの型パラメータを表している場合に、宣言するメソッドを表す MethodInfo を取得します。
パブリック プロパティ DeclaringType オーバーライドされます。 ジェネリック型パラメータが属するジェネリック型の定義、またはジェネリック メソッドの定義を取得します。
パブリック プロパティ DefaultBinder  既定のバインダへの参照を取得します。このバインダは、InvokeMember によって呼び出される適切なメンバを選択するための内部規則を実装します。 ( Type から継承されます。)
パブリック プロパティ FullName オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を取得します。
パブリック プロパティ GenericParameterAttributes  現在のジェネリック型パラメータの共変性および特殊な制約を説明する GenericParameterAttributes フラグの組み合わせを取得します。 ( Type から継承されます。)
パブリック プロパティ GenericParameterPosition オーバーライドされます。 パラメータを宣言したジェネリック型またはジェネリック メソッドの型パラメータ リスト内の型パラメータの位置を取得します。
パブリック プロパティ GUID オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック プロパティ HasElementType  現在の Type が別の型を包含または参照しているかどうか、つまり現在の Type が配列、ポインタ、または参照渡しかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsAbstract  Type が抽象型で、オーバーライドする必要があるかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsAnsiClass  Type に、文字列書式属性として AnsiClass が選択されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsArray  Type が配列かどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsAutoClass  Type に、文字列書式属性として AutoClass が選択されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsAutoLayout  Type に、クラスのレイアウト属性として AutoLayout が選択されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsByRef  Type が参照渡しかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsClass  Type がクラスであり、値型やインターフェイスでないかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsCOMObject  Type が COM オブジェクトかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsContextful  Type をコンテキスト内で管理できるかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsEnum  現在の Type が列挙体であるどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsExplicitLayout  Type に、クラスのレイアウト属性として ExplicitLayout が選択されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsGenericParameter オーバーライドされます。 常に true を取得します。
パブリック プロパティ IsGenericType オーバーライドされます。 常に false を返します。
パブリック プロパティ IsGenericTypeDefinition オーバーライドされます。 常に false を取得します。
パブリック プロパティ IsImport  Type が ComImportAttribute 属性を持っているかどうかを示す (つまり、COM タイプ ライブラリからインポートされたかどうかを示す) 値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsInterface  Type がインターフェイスであり、クラスや値型でないかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsLayoutSequential  Type に、クラスのレイアウト属性として SequentialLayout が選択されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsMarshalByRef  Type が参照渡しでマーシャリングされるかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNested  現在の Type オブジェクトが、別の型の定義内に入れ子になっている定義で定義された型を表しているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNestedAssembly  Type が入れ子になっていて、それ自体が属するアセンブリ内でだけ参照可能かどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNestedFamANDAssem  Type が入れ子になっていて、それ自体が属するファミリとアセンブリの両方に属しているクラスだけから参照可能かどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNestedFamily  Type が入れ子になっていて、それ自体が属するファミリ内でだけ参照可能かどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNestedFamORAssem  Type が入れ子になっていて、それ自体が属するファミリまたはアセンブリのいずれかに属しているクラスだけから参照可能かどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNestedPrivate  Type が入れ子になっていて、プライベートとして宣言されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNestedPublic  クラスが入れ子になっていて、パブリックとして宣言されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsNotPublic  Type がパブリックとして宣言されていないかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsPointer  Type がポインタかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsPrimitive  Type がプリミティブ型の 1 つかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsPublic  Type がパブリックとして宣言されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsSealed  Type が sealed として宣言されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsSerializable  Type をシリアル化できるかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsSpecialName  Type の名前が特別な処理を必要とするかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsUnicodeClass  Type に、文字列書式属性として UnicodeClass が選択されているかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsValueType  Type が値型かどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ IsVisible  Type にアセンブリの外側のコードからアクセスできるかどうかを示す値を取得します。 ( Type から継承されます。)
パブリック プロパティ MemberType  このメンバが型であるか、または入れ子にされた型であるかを示す MemberTypes 値を取得します。 ( Type から継承されます。)
パブリック プロパティ MetadataToken  メタデータ要素を識別する値を取得します。 ( MemberInfo から継承されます。)
パブリック プロパティ Module オーバーライドされます。 ジェネリック型パラメータを格納する動的モジュールを取得します。
パブリック プロパティ Name オーバーライドされます。 ジェネリック型パラメータの名前を取得します。
パブリック プロパティ Namespace オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を取得します。
パブリック プロパティ ReflectedType オーバーライドされます。 GenericTypeParameterBuilder を取得するために使用した Type オブジェクトを取得します。
パブリック プロパティ StructLayoutAttribute  現在の型のレイアウトを説明する StructLayoutAttribute を取得します。 ( Type から継承されます。)
パブリック プロパティ TypeHandle オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック プロパティ TypeInitializer  Type のクラス初期化子を取得します。 ( Type から継承されます。)
パブリック プロパティ UnderlyingSystemType オーバーライドされます。 不完全なジェネリック型パラメータには有効ではありません。
参照参照

関連項目

GenericTypeParameterBuilder クラス
System.Reflection.Emit 名前空間
TypeBuilder.DefineGenericParameters

その他の技術情報

方法 : リフレクション出力を使用してジェネリック型を定義する

GenericTypeParameterBuilder メソッド


パブリック メソッドパブリック メソッド

  名前 説明
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。 2 つのオブジェクトが等しいかどうかを比較します。
パブリック メソッド FindInterfaces  現在の Type によって実装または継承されているインターフェイスのフィルタ適用済みリストを表す、Type オブジェクトの配列を返します。 ( Type から継承されます。)
パブリック メソッド FindMembers  指定したメンバ型の MemberInfo オブジェクトの配列にフィルタを適用して返します。 ( Type から継承されます。)
パブリック メソッド GetArrayRank  Array の次元数を取得します。 ( Type から継承されます。)
パブリック メソッド GetConstructor  オーバーロードされます。 現在の Type の特定のコンストラクタを取得します。 ( Type から継承されます。)
パブリック メソッド GetConstructors オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetCustomAttributes オーバーロードされます。 オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetDefaultMembers  DefaultMemberAttribute が設定されている現在の Type に定義されているメンバを検索します。 ( Type から継承されます。)
パブリック メソッド GetElementType オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を返します。
パブリック メソッド GetEvent オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetEvents オーバーロードされます。 オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetField オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetFields オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetGenericArguments オーバーライドされます。 ジェネリック型パラメータには有効ではありません。
パブリック メソッド GetGenericParameterConstraints  現在のジェネリック型パラメータの制約を表す Type オブジェクトを返します。 ( Type から継承されます。)
パブリック メソッド GetGenericTypeDefinition オーバーライドされます。 ジェネリック型パラメータには有効ではありません。
パブリック メソッド GetHashCode オーバーライドされます。 現在のインスタンスの 32 ビット整数ハッシュ コードを返します。
パブリック メソッド GetInterface オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetInterfaceMap オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetInterfaces オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetMember オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetMembers オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetMethod  オーバーロードされます。 現在の Type の特定のメソッドを取得します。 ( Type から継承されます。)
パブリック メソッド GetMethods オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetNestedType オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetNestedTypes オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetProperties オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetProperty  オーバーロードされます。 現在の Type の特定のプロパティを取得します。 ( Type から継承されます。)
パブリック メソッド GetType  オーバーロードされます。 指定された型を表す Type オブジェクトを取得します。 ( Type から継承されます。)
パブリック メソッド GetTypeArray  指定した配列内のオブジェクトの型を取得します。 ( Type から継承されます。)
パブリック メソッド GetTypeCode  指定した Type の基になる型コードを取得します。 ( Type から継承されます。)
パブリック メソッド GetTypeFromCLSID  オーバーロードされます。 指定したクラス ID (CLSID) に関連付けられている型を取得します。 ( Type から継承されます。)
パブリック メソッド GetTypeFromHandle  指定した型ハンドルによって参照される型を取得します。 ( Type から継承されます。)
パブリック メソッド GetTypeFromProgID  オーバーロードされます。 指定したプログラム ID (ProgID) に関連付けられている型を取得します。 ( Type から継承されます。)
パブリック メソッド GetTypeHandle  指定したオブジェクトの Type のハンドルを取得します。 ( Type から継承されます。)
パブリック メソッド InvokeMember オーバーロードされます。 ジェネリック型パラメータではサポートされていません。
パブリック メソッド IsAssignableFrom オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド IsDefined オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド IsInstanceOfType  指定したオブジェクトが現在の Type のインスタンスかどうかを判断します。 ( Type から継承されます。)
パブリック メソッド IsSubclassOf オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド MakeArrayType オーバーロードされます。 オーバーライドされます。 要素型がジェネリック型パラメータである配列の型を返します。
パブリック メソッド MakeByRefType オーバーライドされます。 参照パラメータとして渡されるときに、現在のジェネリック型パラメータを表す Type オブジェクトを返します。
パブリック メソッド MakeGenericType オーバーライドされます。 不完全なジェネリック型パラメータには有効ではありません。
パブリック メソッド MakePointerType オーバーライドされます。 現在のジェネリック型パラメータへのポインタを表す Type オブジェクトを返します。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ReflectionOnlyGetType  大文字と小文字を区別する検索を実行し、型が見つからない場合に例外をスローするかどうかを指定して、指定した名前の Type を取得します。型は実行ではなくリフレクションのためだけに読み込まれます。 ( Type から継承されます。)
パブリック メソッド SetBaseTypeConstraint 型パラメータを置き換えるために、型が継承する必要のある基本型を設定します。
パブリック メソッド SetCustomAttribute オーバーロードされます。 カスタム属性を設定します。
パブリック メソッド SetGenericParameterAttributes パラメータなしのコンストラクタの制約など、ジェネリック パラメータの分散特性と特殊な制約を設定します。
パブリック メソッド SetInterfaceConstraints 型パラメータを置き換えるために、型が実装する必要のあるインターフェイスを設定します。
パブリック メソッド ToString オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
参照参照

関連項目

GenericTypeParameterBuilder クラス
System.Reflection.Emit 名前空間
TypeBuilder.DefineGenericParameters

その他の技術情報

方法 : リフレクション出力を使用してジェネリック型を定義する

GenericTypeParameterBuilder メンバ

動的に定義されたジェネリック型およびジェネリック メソッドのジェネリック型パラメータを定義および作成します。このクラスは継承できません。

GenericTypeParameterBuilder データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Assembly オーバーライドされます。 現在の型パラメータが属するジェネリック型の定義を格納する動的アセンブリを表す Assembly オブジェクトを取得します。
パブリック プロパティ AssemblyQualifiedName オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を取得します。
パブリック プロパティ Attributes  Type に関連付けられている属性を取得します。(Type から継承されます。)
パブリック プロパティ BaseType オーバーライドされます。 現在のジェネリック型パラメータの基本型の制約を取得します。
パブリック プロパティ ContainsGenericParameters オーバーライドされます。 常に false を取得します。
パブリック プロパティ DeclaringMethod オーバーライドされます。 現在の GenericTypeParameterBuilder がジェネリック メソッドの型パラメータを表している場合に、宣言するメソッドを表す MethodInfo を取得します。
パブリック プロパティ DeclaringType オーバーライドされます。 ジェネリック型パラメータが属するジェネリック型の定義、またはジェネリック メソッドの定義を取得します。
パブリック プロパティ DefaultBinder  既定のバインダへの参照を取得します。このバインダは、InvokeMember によって呼び出される適切なメンバを選択するための内部規則を実装します。(Type から継承されます。)
パブリック プロパティ FullName オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を取得します。
パブリック プロパティ GenericParameterAttributes  現在のジェネリック型パラメータの共変性および特殊な制約を説明する GenericParameterAttributes フラグの組み合わせを取得します。 (Type から継承されます。)
パブリック プロパティ GenericParameterPosition オーバーライドされます。 パラメータを宣言したジェネリック型またはジェネリック メソッドの型パラメータ リスト内の型パラメータの位置を取得します。
パブリック プロパティ GUID オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック プロパティ HasElementType  現在の Type が別の型を包含または参照しているかどうか、つまり現在の Type が配列、ポインタ、または参照渡しかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsAbstract  Type が抽象型で、オーバーライドする必要があるかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsAnsiClass  Type に、文字列書式属性として AnsiClass が選択されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsArray  Type が配列かどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsAutoClass  Type に、文字列書式属性として AutoClass が選択されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsAutoLayout  Type に、クラスのレイアウト属性として AutoLayout が選択されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsByRef  Type が参照渡しかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsClass  Type がクラスであり、値型やインターフェイスでないかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsCOMObject  Type が COM オブジェクトかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsContextful  Type をコンテキスト内で管理できるかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsEnum  現在の Type が列挙体であるどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsExplicitLayout  Type に、クラスのレイアウト属性として ExplicitLayout が選択されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsGenericParameter オーバーライドされます。 常に true を取得します。
パブリック プロパティ IsGenericType オーバーライドされます。 常に false を返します。
パブリック プロパティ IsGenericTypeDefinition オーバーライドされます。 常に false を取得します。
パブリック プロパティ IsImport  Type が ComImportAttribute 属性を持っているかどうかを示す (つまり、COM タイプ ライブラリからインポートされたかどうかを示す) 値を取得します。(Type から継承されます。)
パブリック プロパティ IsInterface  Type がインターフェイスであり、クラスや値型でないかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsLayoutSequential  Type に、クラスのレイアウト属性として SequentialLayout が選択されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsMarshalByRef  Type が参照渡しでマーシャリングされるかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNested  現在の Type オブジェクトが、別の型の定義内に入れ子になっている定義で定義された型を表しているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNestedAssembly  Type が入れ子になっていて、それ自体が属するアセンブリ内でだけ参照可能かどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNestedFamANDAssem  Type が入れ子になっていて、それ自体が属するファミリとアセンブリの両方に属しているクラスだけから参照可能かどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNestedFamily  Type が入れ子になっていて、それ自体が属するファミリ内でだけ参照可能かどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNestedFamORAssem  Type が入れ子になっていて、それ自体が属するファミリまたはアセンブリのいずれかに属しているクラスだけから参照可能かどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNestedPrivate  Type が入れ子になっていて、プライベートとして宣言されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNestedPublic  クラスが入れ子になっていて、パブリックとして宣言されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsNotPublic  Type がパブリックとして宣言されていないかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsPointer  Type がポインタかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsPrimitive  Type がプリミティブ型の 1 つかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsPublic  Type がパブリックとして宣言されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsSealed  Type が sealed として宣言されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsSerializable  Type をシリアル化できるかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsSpecialName  Type の名前が特別な処理を必要とするかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsUnicodeClass  Type に、文字列書式属性として UnicodeClass が選択されているかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsValueType  Type が値型かどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ IsVisible  Type にアセンブリの外側のコードからアクセスできるかどうかを示す値を取得します。(Type から継承されます。)
パブリック プロパティ MemberType  このメンバが型であるか、または入れ子にされた型であるかを示す MemberTypes 値を取得します。(Type から継承されます。)
パブリック プロパティ MetadataToken  メタデータ要素を識別する値を取得します。(MemberInfo から継承されます。)
パブリック プロパティ Module オーバーライドされます。 ジェネリック型パラメータを格納する動的モジュールを取得します。
パブリック プロパティ Name オーバーライドされます。 ジェネリック型パラメータの名前を取得します。
パブリック プロパティ Namespace オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を取得します。
パブリック プロパティ ReflectedType オーバーライドされます。 GenericTypeParameterBuilder を取得するために使用した Type オブジェクトを取得します。
パブリック プロパティ StructLayoutAttribute  現在の型のレイアウトを説明する StructLayoutAttribute を取得します。(Type から継承されます。)
パブリック プロパティ TypeHandle オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック プロパティ TypeInitializer  Type のクラス初期化子を取得します。(Type から継承されます。)
パブリック プロパティ UnderlyingSystemType オーバーライドされます。 不完全なジェネリック型パラメータには有効ではありません。
パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド Equals オーバーロードされます。 オーバーライドされます。 2 つのオブジェクトが等しいかどうかを比較します。
パブリック メソッド FindInterfaces  現在の Type によって実装または継承されているインターフェイスのフィルタ適用済みリストを表す、Type オブジェクトの配列を返します。 (Type から継承されます。)
パブリック メソッド FindMembers  指定したメンバ型の MemberInfo オブジェクトの配列にフィルタを適用して返します。 (Type から継承されます。)
パブリック メソッド GetArrayRank  Array の次元数を取得します。 (Type から継承されます。)
パブリック メソッド GetConstructor  オーバーロードされます。 現在の Type の特定のコンストラクタを取得します。 (Type から継承されます。)
パブリック メソッド GetConstructors オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetCustomAttributes オーバーロードされます。 オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetDefaultMembers  DefaultMemberAttribute が設定されている現在の Type に定義されているメンバを検索します。 (Type から継承されます。)
パブリック メソッド GetElementType オーバーライドされます。 常に null 参照 (Visual Basic では Nothing) を返します。
パブリック メソッド GetEvent オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetEvents オーバーロードされます。 オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetField オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetFields オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetGenericArguments オーバーライドされます。 ジェネリック型パラメータには有効ではありません。
パブリック メソッド GetGenericParameterConstraints  現在のジェネリック型パラメータの制約を表す Type オブジェクトを返します。 (Type から継承されます。)
パブリック メソッド GetGenericTypeDefinition オーバーライドされます。 ジェネリック型パラメータには有効ではありません。
パブリック メソッド GetHashCode オーバーライドされます。 現在のインスタンスの 32 ビット整数ハッシュ コードを返します。
パブリック メソッド GetInterface オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetInterfaceMap オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetInterfaces オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetMember オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetMembers オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetMethod  オーバーロードされます。 現在の Type の特定のメソッドを取得します。 (Type から継承されます。)
パブリック メソッド GetMethods オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetNestedType オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetNestedTypes オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetProperties オーバーロードされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド GetProperty  オーバーロードされます。 現在の Type の特定のプロパティを取得します。 (Type から継承されます。)
パブリック メソッド GetType  オーバーロードされます。 指定された型を表す Type オブジェクトを取得します。 (Type から継承されます。)
パブリック メソッド GetTypeArray  指定した配列内のオブジェクトの型を取得します。 (Type から継承されます。)
パブリック メソッド GetTypeCode  指定した Type の基になる型コードを取得します。 (Type から継承されます。)
パブリック メソッド GetTypeFromCLSID  オーバーロードされます。 指定したクラス ID (CLSID) に関連付けられている型を取得します。 (Type から継承されます。)
パブリック メソッド GetTypeFromHandle  指定した型ハンドルによって参照される型を取得します。 (Type から継承されます。)
パブリック メソッド GetTypeFromProgID  オーバーロードされます。 指定したプログラム ID (ProgID) に関連付けられている型を取得します。 (Type から継承されます。)
パブリック メソッド GetTypeHandle  指定したオブジェクトの Type のハンドルを取得します。 (Type から継承されます。)
パブリック メソッド InvokeMember オーバーロードされます。 ジェネリック型パラメータではサポートされていません。
パブリック メソッド IsAssignableFrom オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド IsDefined オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド IsInstanceOfType  指定したオブジェクトが現在の Type のインスタンスかどうかを判断します。 (Type から継承されます。)
パブリック メソッド IsSubclassOf オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
パブリック メソッド MakeArrayType オーバーロードされます。 オーバーライドされます。 要素型がジェネリック型パラメータである配列の型を返します。
パブリック メソッド MakeByRefType オーバーライドされます。 参照パラメータとして渡されるときに、現在のジェネリック型パラメータを表す Type オブジェクトを返します。
パブリック メソッド MakeGenericType オーバーライドされます。 不完全なジェネリック型パラメータには有効ではありません。
パブリック メソッド MakePointerType オーバーライドされます。 現在のジェネリック型パラメータへのポインタを表す Type オブジェクトを返します。
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ReflectionOnlyGetType  大文字と小文字を区別する検索を実行し、型が見つからない場合に例外をスローするかどうかを指定して、指定した名前の Type を取得します。型は実行ではなくリフレクションのためだけに読み込まれます。 (Type から継承されます。)
パブリック メソッド SetBaseTypeConstraint 型パラメータを置き換えるために、型が継承する必要のある基本型を設定します。
パブリック メソッド SetCustomAttribute オーバーロードされます。 カスタム属性を設定します。
パブリック メソッド SetGenericParameterAttributes パラメータなしのコンストラクタの制約など、ジェネリック パラメータの分散特性と特殊な制約を設定します。
パブリック メソッド SetInterfaceConstraints 型パラメータを置き換えるために、型が実装する必要のあるインターフェイスを設定します。
パブリック メソッド ToString オーバーライドされます。 不完全なジェネリック型パラメータではサポートされていません。
参照参照

関連項目

GenericTypeParameterBuilder クラス
System.Reflection.Emit 名前空間
TypeBuilder.DefineGenericParameters

その他の技術情報

方法 : リフレクション出力を使用してジェネリック型を定義する



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「GenericTypeParameterBuilder」の関連用語

GenericTypeParameterBuilderのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



GenericTypeParameterBuilderのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS