MethodBuilder.DefineGenericParameters メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > MethodBuilder.DefineGenericParameters メソッドの意味・解説 

MethodBuilder.DefineGenericParameters メソッド

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

現在のメソッドジェネリック型パラメータの数を設定し、その名前を指定し制約の定義に使用できる GenericTypeParameterBuilder オブジェクト配列返します

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

Public Function DefineGenericParameters ( _
    ParamArray names As String()
 _
) As GenericTypeParameterBuilder()
Dim instance As MethodBuilder
Dim names As String()
Dim returnValue As GenericTypeParameterBuilder()

returnValue = instance.DefineGenericParameters(names)
public GenericTypeParameterBuilder[] DefineGenericParameters (
    params string[] names
)
public:
array<GenericTypeParameterBuilder^>^ DefineGenericParameters (
    ... array<String^>^ names
)
public GenericTypeParameterBuilder[] DefineGenericParameters (
    String[] names
)
public function DefineGenericParameters (
    ... names : String[]
) : GenericTypeParameterBuilder[]

パラメータ

names

ジェネリック型パラメータの名前を表す文字列の配列

戻り値
ジェネリック メソッド型パラメータを表す GenericTypeParameterBuilder オブジェクト配列

例外例外
例外種類条件

InvalidOperationException

ジェネリック型パラメータは、このメソッドに対して既に定義されています。

または

メソッドは既に完了してます。

または

SetImplementationFlags メソッド現在のメソッドに対して呼び出されています。

ArgumentNullException

namesnull 参照 (Visual Basic では Nothing) です。

または

names要素null 参照 (Visual Basic では Nothing) です。

ArgumentException

names が空の配列です。

解説解説

DefineGenericParameters メソッド呼び出すと、現在のメソッドジェネリックなります。この変更元に戻す方法はありません。このメソッド2 回目呼び出しによって、InvalidOperationException発生します

ジェネリック メソッド型パラメータは、GetGenericArguments メソッド使用して後で取得できます

規約では、型パラメータ名は単一の英大文字です。

詳細については、MethodInfo.IsGenericMethod、MethodInfo.GetGenericMethodDefinition の各トピック参照してくださいジェネリック型については、Type.IsGenericType に関するトピック参照してください

使用例使用例

動的型 DemoType作成し動的ジェネリック メソッド DemoMethod格納するコード例次に示します。このメソッドには、2 つジェネリック型パラメータあります1 つパラメータとして使用され、もう 1 つ戻り値の型として使用されます。

コード実行時に、動的アセンブリは DemoGenericMethod1.dll という名前で保存されます。このアセンブリは、MSIL 逆アセンブラ (Ildasm.exe) を使用してチェックできます

メモメモ

このコード例では、null 参照返すだけの単純なメソッド本体生成されます。ジェネリック型作成して使用する、より高度なメソッド本体コード例については、「方法 : リフレクション出力使用してジェネリック メソッド定義する」を参照してください

Imports System
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)
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)
*/
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)
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
MethodBuilder クラス
MethodBuilder メンバ
System.Reflection.Emit 名前空間
MethodInfo クラス
Type.IsGenericType
System.Reflection.Emit.TypeBuilder.DefineMethod
その他の技術情報
方法 : リフレクション出力使用してジェネリック メソッド定義する



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「MethodBuilder.DefineGenericParameters メソッド」の関連用語

MethodBuilder.DefineGenericParameters メソッドのお隣キーワード
検索ランキング

   

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



MethodBuilder.DefineGenericParameters メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS