MethodBuilder.DefineGenericParameters(String[]) 메서드

정의

현재 메서드에 대한 제네릭 형식 매개 변수의 개수를 설정하고 이러한 매개 변수의 이름을 지정하며 관련 제약 조건을 정의하는 데 사용할 수 있는 GenericTypeParameterBuilder 개체의 배열을 반환합니다.

public:
 cli::array <System::Reflection::Emit::GenericTypeParameterBuilder ^> ^ DefineGenericParameters(... cli::array <System::String ^> ^ names);
public System.Reflection.Emit.GenericTypeParameterBuilder[] DefineGenericParameters (params string[] names);
member this.DefineGenericParameters : string[] -> System.Reflection.Emit.GenericTypeParameterBuilder[]
Public Function DefineGenericParameters (ParamArray names As String()) As GenericTypeParameterBuilder()

매개 변수

names
String[]

제네릭 형식 매개 변수의 이름을 나타내는 문자열의 배열입니다.

반환

제네릭 메서드의 형식 매개 변수를 나타내는 GenericTypeParameterBuilder 개체의 배열입니다.

예외

제네릭 형식 매개 변수가 이 메서드에 대해 이미 정의되었습니다.

또는

메서드가 이미 완료된 경우

또는

현재 메서드에 대해 SetImplementationFlags(MethodImplAttributes) 메서드가 호출되었습니다.

names이(가) null인 경우

또는

names의 요소가 null입니다.

names가 빈 배열인 경우

예제

다음 코드 예제에서는 동적 제네릭 메서드 DemoMethod를 포함하는 동적 형식 DemoType을 만듭니다. 이 메서드에는 두 개의 제네릭 형식 매개 변수가 있으며, 그 중 하나는 매개 변수로 사용되고 다른 하나는 반환 형식으로 사용됩니다.

코드를 실행하면 동적 어셈블리가 DemoGenericMethod1.dll로 저장되며 Ildasm.exe(IL 디스어셈블러)를 사용하여 검사할 수 있습니다.

참고

이 코드 예제에서는 null 참조만 반환하는 간단한 메서드 본문을 생성합니다. 제네릭 형식을 만들고 사용하는 완전히 개발된 메서드 본문이 있는 코드 예제는 방법: 리플렉션 내보내기를 사용하여 제네릭 메서드 정의를 참조하세요.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

public ref class GenericReflectionSample
{
};

int main()
{
    // Creating a dynamic assembly requires an AssemblyName
    // object, and the current application domain.
    //
    AssemblyName^ asmName =
        gcnew AssemblyName("EmittedAssembly");
    AppDomain^ domain = AppDomain::CurrentDomain;
    AssemblyBuilder^ sampleAssemblyBuilder =
        domain->DefineDynamicAssembly(asmName,
        AssemblyBuilderAccess::RunAndSave);

    // Define the module that contains the code. For an
    // assembly with one module, the module name is the
    // assembly name plus a file extension.
    ModuleBuilder^ sampleModuleBuilder =
        sampleAssemblyBuilder->DefineDynamicModule(asmName->Name,
        asmName->Name + ".dll");

    TypeBuilder^ sampleTypeBuilder =
        sampleModuleBuilder->DefineType("SampleType",
        TypeAttributes::Public | TypeAttributes::Abstract);

    // Define a Shared, Public method with standard calling
    // conventions. Do not specify the parameter types or the
    // return type, because type parameters will be used for
    // those types, and the type parameters have not been
    // defined yet.
    MethodBuilder^ sampleMethodBuilder =
        sampleTypeBuilder->DefineMethod("SampleMethod",
        MethodAttributes::Public | MethodAttributes::Static);

    // Defining generic parameters for the method makes it a
    // generic method. By convention, type parameters are
    // single alphabetic characters. T and U are used here.
    //
    array<String^>^ genericTypeNames = {"T", "U"};
    array<GenericTypeParameterBuilder^>^ genericTypes =
        sampleMethodBuilder->DefineGenericParameters(
        genericTypeNames);

    // Use the IsGenericMethod property to find out if a
    // dynamic method is generic, and IsGenericMethodDefinition
    // to find out if it defines a generic method.
    Console::WriteLine("Is SampleMethod generic? {0}",
        sampleMethodBuilder->IsGenericMethod);
    Console::WriteLine(
        "Is SampleMethod a generic method definition? {0}",
        sampleMethodBuilder->IsGenericMethodDefinition);

    // Set parameter types for the method. The method takes
    // one parameter, and its type is specified by the first
    // type parameter, T.
    array<Type^>^ parameterTypes = {genericTypes[0]};
    sampleMethodBuilder->SetParameters(parameterTypes);

    // Set the return type for the method. The return type is
    // specified by the second type parameter, U.
    sampleMethodBuilder->SetReturnType(genericTypes[1]);

    // Generate a code body for the method. The method doesn't
    // do anything except return null.
    //
    ILGenerator^ ilgen = sampleMethodBuilder->GetILGenerator();
    ilgen->Emit(OpCodes::Ldnull);
    ilgen->Emit(OpCodes::Ret);

    // Complete the type.
    Type^ sampleType = sampleTypeBuilder->CreateType();

    // To bind types to a dynamic generic method, you must
    // first call the GetMethod method on the completed type.
    // You can then define an array of types, and bind them
    // to the method.
    MethodInfo^ sampleMethodInfo = sampleType->GetMethod("SampleMethod");
    array<Type^>^ boundParameters =
        {String::typeid, GenericReflectionSample::typeid};
    MethodInfo^ boundMethodInfo =
        sampleMethodInfo->MakeGenericMethod(boundParameters);
    // Display a string representing the bound method.
    Console::WriteLine(boundMethodInfo);

    // Save the assembly, so it can be examined with Ildasm.exe.
    sampleAssemblyBuilder->Save(asmName->Name + ".dll");
}

/* This code example produces the following output:
Is SampleMethod generic? True
Is SampleMethod a generic method definition? True
GenericReflectionSample SampleMethod[String,GenericReflectionSample](System.String)
*/
using System;
using System.Reflection;
using System.Reflection.Emit;

class DemoMethodBuilder
{

    public static void Main()
    {
        // Creating a dynamic assembly requires an AssemblyName
        // object, and the current application domain.
        //
        AssemblyName asmName =
            new AssemblyName("DemoMethodBuilder1");
        AppDomain domain = AppDomain.CurrentDomain;
        AssemblyBuilder demoAssembly =
            domain.DefineDynamicAssembly(
                asmName,
                AssemblyBuilderAccess.RunAndSave
            );

        // Define the module that contains the code. For an
        // assembly with one module, the module name is the
        // assembly name plus a file extension.
        ModuleBuilder demoModule =
            demoAssembly.DefineDynamicModule(
                asmName.Name,
                asmName.Name + ".dll"
            );

        TypeBuilder demoType = demoModule.DefineType(
            "DemoType",
            TypeAttributes.Public | TypeAttributes.Abstract
        );

        // Define a Shared, Public method with standard calling
        // conventions. Do not specify the parameter types or the
        // return type, because type parameters will be used for
        // those types, and the type parameters have not been
        // defined yet.
        MethodBuilder demoMethod = demoType.DefineMethod(
            "DemoMethod",
            MethodAttributes.Public | MethodAttributes.Static
        );

        // Defining generic parameters for the method makes it a
        // generic method. By convention, type parameters are
        // single alphabetic characters. T and U are used here.
        //
        string[] typeParamNames = {"T", "U"};
        GenericTypeParameterBuilder[] typeParameters =
            demoMethod.DefineGenericParameters(typeParamNames);

        // The second type parameter is constrained to be a
        // reference type.
        typeParameters[1].SetGenericParameterAttributes(
            GenericParameterAttributes.ReferenceTypeConstraint);

        // Use the IsGenericMethod property to find out if a
        // dynamic method is generic, and IsGenericMethodDefinition
        // to find out if it defines a generic method.
        Console.WriteLine("Is DemoMethod generic? {0}",
            demoMethod.IsGenericMethod);
        Console.WriteLine("Is DemoMethod a generic method definition? {0}",
            demoMethod.IsGenericMethodDefinition);

        // Set parameter types for the method. The method takes
        // one parameter, and its type is specified by the first
        // type parameter, T.
        Type[] parms = {typeParameters[0]};
        demoMethod.SetParameters(parms);

        // Set the return type for the method. The return type is
        // specified by the second type parameter, U.
        demoMethod.SetReturnType(typeParameters[1]);

        // Generate a code body for the method. The method doesn't
        // do anything except return null.
        //
        ILGenerator ilgen = demoMethod.GetILGenerator();
        ilgen.Emit(OpCodes.Ldnull);
        ilgen.Emit(OpCodes.Ret);

        // Complete the type.
        Type dt = demoType.CreateType();

        // To bind types to a dynamic generic method, you must
        // first call the GetMethod method on the completed type.
        // You can then define an array of types, and bind them
        // to the method.
        MethodInfo m = dt.GetMethod("DemoMethod");
        Type[] typeArgs = {typeof(string), typeof(DemoMethodBuilder)};
        MethodInfo bound = m.MakeGenericMethod(typeArgs);
        // Display a string representing the bound method.
        Console.WriteLine(bound);

        // Save the assembly, so it can be examined with Ildasm.exe.
        demoAssembly.Save(asmName.Name + ".dll");
    }
}

/* This code example produces the following output:
Is DemoMethod generic? True
Is DemoMethod a generic method definition? True
DemoMethodBuilder DemoMethod[String,DemoMethodBuilder](System.String)
*/
Imports System.Reflection
Imports System.Reflection.Emit

Class DemoMethodBuilder
   
    Public Shared Sub Main()
        ' Creating a dynamic assembly requires an AssemblyName
        ' object, and the current application domain.
        '
        Dim asmName As New AssemblyName("DemoMethodBuilder1")
        Dim domain As AppDomain = AppDomain.CurrentDomain
        Dim demoAssembly As AssemblyBuilder = _
            domain.DefineDynamicAssembly(asmName, _
                AssemblyBuilderAccess.RunAndSave)

        ' Define the module that contains the code. For an 
        ' assembly with one module, the module name is the 
        ' assembly name plus a file extension.
        Dim demoModule As ModuleBuilder = _
            demoAssembly.DefineDynamicModule( _
                asmName.Name, _
                asmName.Name & ".dll")
      
        Dim demoType As TypeBuilder = demoModule.DefineType( _
            "DemoType", _
            TypeAttributes.Public Or TypeAttributes.Abstract)

        ' Define a Shared, Public method with standard calling
        ' conventions. Do not specify the parameter types or the
        ' return type, because type parameters will be used for 
        ' those types, and the type parameters have not been
        ' defined yet.
        Dim demoMethod As MethodBuilder = _
            demoType.DefineMethod("DemoMethod", _
                MethodAttributes.Public Or MethodAttributes.Static)

        ' Defining generic parameters for the method makes it a
        ' generic method. By convention, type parameters are 
        ' single alphabetic characters. T and U are used here.
        '
        Dim typeParamNames() As String = {"T", "U"}
        Dim typeParameters() As GenericTypeParameterBuilder = _
            demoMethod.DefineGenericParameters(typeParamNames)

        ' The second type parameter is constrained to be a 
        ' reference type.
        typeParameters(1).SetGenericParameterAttributes( _
            GenericParameterAttributes.ReferenceTypeConstraint)

        ' Use the IsGenericMethod property to find out if a
        ' dynamic method is generic, and IsGenericMethodDefinition
        ' to find out if it defines a generic method.
        Console.WriteLine("Is DemoMethod generic? {0}", _
            demoMethod.IsGenericMethod)
        Console.WriteLine("Is DemoMethod a generic method definition? {0}", _
            demoMethod.IsGenericMethodDefinition)

        ' Set parameter types for the method. The method takes
        ' one parameter, and its type is specified by the first
        ' type parameter, T.
        Dim params() As Type = {typeParameters(0)}
        demoMethod.SetParameters(params)

        ' Set the return type for the method. The return type is
        ' specified by the second type parameter, U.
        demoMethod.SetReturnType(typeParameters(1))

        ' Generate a code body for the method. The method doesn't
        ' do anything except return Nothing.
        '
        Dim ilgen As ILGenerator = demoMethod.GetILGenerator()
        ilgen.Emit(OpCodes.Ldnull)
        ilgen.Emit(OpCodes.Ret)

        ' Complete the type.
        Dim dt As Type = demoType.CreateType()

        ' To bind types to a dynamic generic method, you must 
        ' first call the GetMethod method on the completed type.
        ' You can then define an array of types, and bind them
        ' to the method.
        Dim m As MethodInfo = dt.GetMethod("DemoMethod")
        Dim typeArgs() As Type = _
            {GetType(String), GetType(DemoMethodBuilder)}
        Dim bound As MethodInfo = m.MakeGenericMethod(typeArgs)
        ' Display a string representing the bound method.
        Console.WriteLine(bound)

        ' Save the assembly, so it can be examined with Ildasm.exe.
        demoAssembly.Save(asmName.Name & ".dll")
    End Sub  
End Class 

' This code example produces the following output:
'Is DemoMethod generic? True
'Is DemoMethod a generic method definition? True
'DemoMethodBuilder DemoMethod[String,DemoMethodBuilder](System.String)

설명

메서드를 호출하면 DefineGenericParameters 현재 메서드가 제네릭으로 만들어집니다. 이 변경 내용을 실행 취소할 수 있는 방법은 없습니다. 이 메서드를 두 번째로 호출하면 가 발생합니다 InvalidOperationException.

제네릭 메서드의 형식 매개 변수는 나중에 메서드를 사용하여 GetGenericArguments 검색할 수 있습니다.

규칙에 따라 형식 매개 변수 이름은 단일 대문자입니다.

자세한 내용은 MethodBase.IsGenericMethodMethodInfo.GetGenericMethodDefinition를 참조하세요. 제네릭 형식에 대한 자세한 내용은 를 참조하세요 Type.IsGenericType.

적용 대상

추가 정보