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

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

MethodBuilder.SetSignature メソッド

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

戻り値の型、パラメータの型、戻り値の型とパラメータの型の必須およびオプションカスタム修飾子を含むメソッド シグネチャ設定します

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

Public Sub SetSignature ( _
    returnType As Type, _
    returnTypeRequiredCustomModifiers As Type(), _
    returnTypeOptionalCustomModifiers As Type(), _
    parameterTypes As Type(), _
    parameterTypeRequiredCustomModifiers As Type()(), _
    parameterTypeOptionalCustomModifiers As Type()() _
)
Dim instance As MethodBuilder
Dim returnType As Type
Dim returnTypeRequiredCustomModifiers As Type()
Dim returnTypeOptionalCustomModifiers As Type()
Dim parameterTypes As Type()
Dim parameterTypeRequiredCustomModifiers As
 Type()()
Dim parameterTypeOptionalCustomModifiers As
 Type()()

instance.SetSignature(returnType, returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers,
 parameterTypes, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers)
public void SetSignature (
    Type returnType,
    Type[] returnTypeRequiredCustomModifiers,
    Type[] returnTypeOptionalCustomModifiers,
    Type[] parameterTypes,
    Type[][] parameterTypeRequiredCustomModifiers,
    Type[][] parameterTypeOptionalCustomModifiers
)
public:
void SetSignature (
    Type^ returnType, 
    array<Type^>^ returnTypeRequiredCustomModifiers, 
    array<Type^>^ returnTypeOptionalCustomModifiers, 
    array<Type^>^ parameterTypes, 
    array<array<Type^>^>^ parameterTypeRequiredCustomModifiers, 
    array<array<Type^>^>^ parameterTypeOptionalCustomModifiers
)
public void SetSignature (
    Type returnType, 
    Type[] returnTypeRequiredCustomModifiers, 
    Type[] returnTypeOptionalCustomModifiers, 
    Type[] parameterTypes, 
    Type[][] parameterTypeRequiredCustomModifiers, 
    Type[][] parameterTypeOptionalCustomModifiers
)
public function SetSignature (
    returnType : Type, 
    returnTypeRequiredCustomModifiers : Type[], 
    returnTypeOptionalCustomModifiers : Type[], 
    parameterTypes : Type[], 
    parameterTypeRequiredCustomModifiers : Type[][], 
    parameterTypeOptionalCustomModifiers : Type[][]
)

パラメータ

returnType

メソッド戻り値の型。

returnTypeRequiredCustomModifiers

メソッド戻り値の型の必須のカスタム修飾子 (IsConst など) を表す型の配列戻り値の型に必須のカスタム修飾子ない場合は、null 参照 (Visual Basic では Nothing) を指定します

returnTypeOptionalCustomModifiers

メソッド戻り値の型のオプションカスタム修飾子 (IsConst など) を表す型の配列戻り値の型に必須のカスタム修飾子ない場合は、null 参照 (Visual Basic では Nothing) を指定します

parameterTypes

メソッドパラメータの型。

parameterTypeRequiredCustomModifiers

型の配列配列。型の各配列は、対応するパラメータ必須のカスタム修飾子 (IsConst など) を表します特定のパラメータ必須のカスタム修飾子ない場合は、型の配列代わりに null 参照 (Visual Basic では Nothing) を指定します必須のカスタム修飾子を持つパラメータない場合は、配列配列代わりに null 参照 (Visual Basic では Nothing) を指定します

parameterTypeOptionalCustomModifiers

型の配列配列。型の各配列は、対応するパラメータオプションカスタム修飾子 (IsConst など) を表します特定のパラメータオプションカスタム修飾子ない場合は、型の配列代わりに null 参照 (Visual Basic では Nothing) を指定しますオプションカスタム修飾子を持つパラメータない場合は、配列配列代わりに null 参照 (Visual Basic では Nothing) を指定します

例外例外
例外種類条件

InvalidOperationException

現在のメソッドジェネリック メソッドですが、ジェネリック メソッドの定義ではありません。つまり、IsGenericMethod プロパティtrue ですが、IsGenericMethodDefinition プロパティfalse です。

解説解説

メソッド定義するときに、戻り値の型およびパラメータの数と型がわからない場合は、パラメータの型の配列受け入れる System.Reflection.Emit.TypeBuilder.DefineMethod メソッド任意のオーバーロード使用してこれらを設定できます。ただし、ジェネリック メソッドは、1 つ上の独自のジェネリック型パラメータで型が指定されパラメータを持つことができます。これらのパラメータは、メソッド定義されるまで定義できません。その場合は、このメソッド使用してパラメータの型を設定します

戻り値の型とパラメータの型のいずれにも、IsConstModifier などのオプションまたは必須のカスタム修飾子ない場合は、SetReturnType メソッドと SetParameters メソッド使用できます

このメソッド呼び出すと、System.Reflection.Emit.TypeBuilder.DefineMethod メソッド使用して設定されパラメータ戻り値の型が置き換えられます。

使用例使用例

型パラメータ T を持つ、Sample という名前のジェネリック クラスソース コードを含むコード例次に示します。このクラスは、Field という名前のフィールドを持つ T 型のクラスです。また、独自の型パラメータ U を持つジェネリック メソッド GM含まれています。GM メソッドは、Sample型パラメータを独自の型パラメータ U置き換えてSampleインスタンス作成し入力パラメータField格納します。このソース コードコンパイルされますが、使用されません。MSIL 逆アセンブラ (Ildasm.exe) を使用してソース コード表示しExample クラス出力されコード比較できます

ジェネリック コード出力時にSetSignature メソッド使用する Example クラスコード示しますExample クラスMain メソッドは、Sample という名前のクラス格納する動的アセンブリ作成し、TypeBuilder.DefineGenericParameters メソッド使用してT という名前の型パラメータ追加し、これをジェネリックにします。既定コンストラクタField という名前の T 型のフィールドが、Sample クラス追加されます。DefineGenericParameters メソッド使用してGM メソッド追加されジェネリック メソッド変更されます。GM型パラメータには、U という名前が付けられています。型パラメータ定義されると、SetSignature メソッド使用してGMシグネチャ追加されます。戻り値の型はなく、必須の修飾子カスタム修飾子存在しないため、メソッド唯一のパラメータの型を設定する parameterTypes除き、このメソッドすべてのパラメータnull 参照 (Visual Basic では Nothing) です。メソッドパラメータの型は、メソッド型パラメータである U設定されます。メソッド本体は、構築された型 Sample<U> (Visual Basic では Sample(Of U)) のインスタンス作成しメソッドパラメータField割り当てた後、Field の値を出力します。エントリ ポイントである Main メソッド保持するために、ダミー型が定義されます。Main本体では、UString 型に置き換えて構築ジェネリック型 Sample<int> (Visual Basic では Sample(Of Integer)) で静的メソッド GM呼び出されます。TypeBuilder.GetMethod メソッド使用して構築ジェネリック型 Sample<U>静的メソッド GM の MethodInfo を作成します次に、MethodInfo.MakeGenericMethod メソッド使用してメソッド呼び出し出力できる MethodInfo作成します

このコード例実行すると、出力されアセンブリは TypeBuilderGetFieldExample.exe として保存されます。この TypeBuilderGetFieldExample.exe を実行できますまた、MSIL 逆アセンブラ (Ildasm.exe) を使用すると、出力したコードをこのコード例自体コンパイルされる Sample クラスコード比較できます

Imports System
Imports System.Reflection
Imports System.Reflection.Emit

' Compare the MSIL in this class to the MSIL
' generated by the Reflection.Emit code in class
' Example.
Public Class Sample(Of T)
    Public Field As T
    Public Shared Sub GM(Of
 U)(ByVal val As U)
        Dim s As New Sample(Of
 U)
        s.Field = val
        Console.WriteLine(s.Field)
    
    End Sub
End Class 

Public Class Example
    
    Public Shared Sub Main()
 
        Dim myDomain As AppDomain = AppDomain.CurrentDomain
        Dim myAsmName As New
 AssemblyName("TypeBuilderGetFieldExample")
        Dim myAssembly As AssemblyBuilder =
 _
            myDomain.DefineDynamicAssembly(myAsmName, _
                AssemblyBuilderAccess.Save)
        Dim myModule As ModuleBuilder = _
            myAssembly.DefineDynamicModule(myAsmName.Name, _
                myAsmName.Name & ".exe")
        
        ' Define the sample type.
        Dim myType As TypeBuilder = myModule.DefineType(
 _
            "Sample", _
            TypeAttributes.Class Or TypeAttributes.Public)
        
        ' Add a type parameter, making the type generic.
        Dim typeParamNames() As String
 = { "T" }
        Dim typeParams As GenericTypeParameterBuilder()
 = _
            myType.DefineGenericParameters(typeParamNames)
        
        ' Define a default constructor. Normally it would 
        ' not be necessary to define the default constructor,
        ' but in this case it is needed for the call to
        ' TypeBuilder.GetConstructor, which gets the default
        ' constructor for the generic type constructed from 
        ' Sample(Of T), in the generic method GM(Of U).
        Dim ctor As ConstructorBuilder = _
            myType.DefineDefaultConstructor( _
                MethodAttributes.PrivateScope Or MethodAttributes.Public
 _
                Or MethodAttributes.HideBySig Or
 MethodAttributes.SpecialName _
                Or MethodAttributes.RTSpecialName)
        
        ' Add a field of type T, with the name Field.
        Dim myField As FieldBuilder = myType.DefineField(
 _
            "Field", typeParams(0), FieldAttributes.Public)
        
        ' Add a method and make it generic, with a type 
        ' parameter named U. Note how similar this is to 
        ' the way Sample is turned into a generic type. The
        ' method has no signature, because the type of its
        ' only parameter is U, which is not yet defined.
        Dim genMethod As MethodBuilder = _
            myType.DefineMethod("GM", _
                MethodAttributes.Public Or MethodAttributes.Static)
        Dim methodParamNames() As String
 = { "U" }
        Dim methodParams As GenericTypeParameterBuilder()
 = _
            genMethod.DefineGenericParameters(methodParamNames)

        ' Now add a signature for genMethod, specifying U
        ' as the type of the parameter. There is no return value
        ' and no custom modifiers.
        genMethod.SetSignature(Nothing, Nothing,
 Nothing, _
            New Type() { methodParams(0) }, Nothing,
 Nothing)
        
        ' Emit a method body for the generic method.
        Dim ilg As ILGenerator = genMethod.GetILGenerator()
        ' Construct the type Sample(Of U) using MakeGenericType.
        Dim SampleOfU As Type = _
            myType.MakeGenericType(methodParams(0))
        ' Create a local variable to store the instance of
        ' Sample(Of U).
        ilg.DeclareLocal(SampleOfU)
        ' Call the default constructor. Note that it is 
        ' necessary to have the default constructor for the
        ' constructed generic type Sample(Of U); use the 
        ' TypeBuilder.GetConstructor method to obtain this 
        ' constructor.
        Dim ctorOfU As ConstructorInfo = _
            TypeBuilder.GetConstructor(SampleOfU, ctor)
        ilg.Emit(OpCodes.Newobj, ctorOfU)
        ' Store the instance in the local variable; load it
        ' again, and load the parameter of genMethod.
        ilg.Emit(OpCodes.Stloc_0)
        ilg.Emit(OpCodes.Ldloc_0)
        ilg.Emit(OpCodes.Ldarg_0)
        ' In order to store the value in the field of the
        ' instance of Sample(Of U), it is necessary to have 
        ' a FieldInfo representing the field of the 
        ' constructed type. Use TypeBuilder.GetField to 
        ' obtain this FieldInfo.
        Dim FieldOfU As FieldInfo = _
            TypeBuilder.GetField(SampleOfU, myField)
        ' Store the value in the field. 
        ilg.Emit(OpCodes.Stfld, FieldOfU)
        ' Load the instance, load the field value, box it
        ' (specifying the type of the type parameter, U), 
        ' and print it.
        ilg.Emit(OpCodes.Ldloc_0)
        ilg.Emit(OpCodes.Ldfld, FieldOfU)
        ilg.Emit(OpCodes.Box, methodParams(0))
        Dim writeLineObj As MethodInfo = _
            GetType(Console).GetMethod("WriteLine",
 _
                New Type() {GetType(Object)})
        ilg.EmitCall(OpCodes.Call, writeLineObj, Nothing)
        ilg.Emit(OpCodes.Ret)
        
        ' Emit an entry point method; this must be in a
        ' non-generic type.
        Dim dummy As TypeBuilder = _
            myModule.DefineType("Dummy", _
                TypeAttributes.Class Or TypeAttributes.NotPublic)
        Dim entryPoint As MethodBuilder = _
            dummy.DefineMethod("Main", _
                MethodAttributes.Public Or MethodAttributes.Static,
 _
                Nothing, Nothing)
        ilg = entryPoint.GetILGenerator()
        ' In order to call the static generic method GM, it is
        ' necessary to create a constructed type from the 
        ' generic type definition for Sample. This can be ANY
        ' constructed type; in this case Sample(Of Integer)
        ' is used.
        Dim SampleOfInt As Type = _
            myType.MakeGenericType(GetType(Integer))
        ' Next get a MethodInfo representing the static generic
        ' method GM on type Sample(Of Integer).
        Dim SampleOfIntGM As MethodInfo = _
            TypeBuilder.GetMethod(SampleOfInt, genMethod)
        ' Next get a MethodInfo for GM(Of String), which is the 
        ' instantiation of generic method GM that is called
        ' by Sub Main.
        Dim GMOfString As MethodInfo = _
            SampleOfIntGM.MakeGenericMethod(GetType(String))
        ' Finally, emit the call. Push a string onto
        ' the stack, as the argument for the generic method.
        ilg.Emit(OpCodes.Ldstr, "Hello, world!")
        ilg.EmitCall(OpCodes.Call, GMOfString, Nothing)
        ilg.Emit(OpCodes.Ret)
        
        myType.CreateType()
        dummy.CreateType()
        myAssembly.SetEntryPoint(entryPoint)
        myAssembly.Save(myAsmName.Name & ".exe")
        
        Console.WriteLine(myAsmName.Name & ".exe has been
 saved.")
    
    End Sub 
End Class 
using System;
using System.Reflection;
using System.Reflection.Emit;

// Compare the MSIL in this class to the MSIL
// generated by the Reflection.Emit code in class
// Example.
public class Sample<T>
{
  public T Field;
  public static void GM<U>(U
 val)
  {
    Sample<U> s = new Sample<U>();
    s.Field = val;
    Console.WriteLine(s.Field);
  }
}

public class Example
{
    public static void Main()
    {
        AppDomain myDomain = AppDomain.CurrentDomain;
        AssemblyName myAsmName = 
            new AssemblyName("TypeBuilderGetFieldExample");
        AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
            myAsmName, AssemblyBuilderAccess.Save);
        ModuleBuilder myModule = myAssembly.DefineDynamicModule(
            myAsmName.Name, 
            myAsmName.Name + ".exe");

        // Define the sample type.
        TypeBuilder myType = myModule.DefineType("Sample", 
            TypeAttributes.Class | TypeAttributes.Public);

        // Add a type parameter, making the type generic.
        string[] typeParamNames = {"T"};  
        GenericTypeParameterBuilder[] typeParams = 
            myType.DefineGenericParameters(typeParamNames);

        // Define a default constructor. Normally it would 
        // not be necessary to define the default constructor,
        // but in this case it is needed for the call to
        // TypeBuilder.GetConstructor, which gets the default
        // constructor for the generic type constructed from 
        // Sample<T>, in the generic method GM<U>.
        ConstructorBuilder ctor = myType.DefineDefaultConstructor(
            MethodAttributes.PrivateScope | MethodAttributes.Public |
            MethodAttributes.HideBySig | MethodAttributes.SpecialName |
            MethodAttributes.RTSpecialName);

        // Add a field of type T, with the name Field.
        FieldBuilder myField = myType.DefineField("Field", 
            typeParams[0],
            FieldAttributes.Public);

        // Add a method and make it generic, with a type 
        // parameter named U. Note how similar this is to 
        // the way Sample is turned into a generic type. The
        // method has no signature, because the type of its
        // only parameter is U, which is not yet defined.
        MethodBuilder genMethod = myType.DefineMethod("GM", 
            MethodAttributes.Public | MethodAttributes.Static);
        string[] methodParamNames = {"U"};
        GenericTypeParameterBuilder[] methodParams = 
            genMethod.DefineGenericParameters(methodParamNames);
        
        // Now add a signature for genMethod, specifying U
        // as the type of the parameter. There is no return value
        // and no custom modifiers.
        genMethod.SetSignature(null, null,
 null, 
            new Type[] { methodParams[0] }, null,
 null);

        // Emit a method body for the generic method.
        ILGenerator ilg = genMethod.GetILGenerator();
        // Construct the type Sample<U> using MakeGenericType.
        Type SampleOfU = myType.MakeGenericType( methodParams[0] );
        // Create a local variable to store the instance of
        // Sample<U>.
        ilg.DeclareLocal(SampleOfU);
        // Call the default constructor. Note that it is 
        // necessary to have the default constructor for the
        // constructed generic type Sample<U>; use the 
        // TypeBuilder.GetConstructor method to obtain this 
        // constructor.
        ConstructorInfo ctorOfU = TypeBuilder.GetConstructor(
            SampleOfU, ctor);
        ilg.Emit(OpCodes.Newobj, ctorOfU);
        // Store the instance in the local variable; load it
        // again, and load the parameter of genMethod.
        ilg.Emit(OpCodes.Stloc_0); 
        ilg.Emit(OpCodes.Ldloc_0); 
        ilg.Emit(OpCodes.Ldarg_0);
        // In order to store the value in the field of the
        // instance of Sample<U>, it is necessary to have 
        // a FieldInfo representing the field of the 
        // constructed type. Use TypeBuilder.GetField to 
        // obtain this FieldInfo.
        FieldInfo FieldOfU = TypeBuilder.GetField(
            SampleOfU, myField);
        // Store the value in the field. 
        ilg.Emit(OpCodes.Stfld, FieldOfU);
        // Load the instance, load the field value, box it
        // (specifying the type of the type parameter, U), and
        // print it.
        ilg.Emit(OpCodes.Ldloc_0);
        ilg.Emit(OpCodes.Ldfld, FieldOfU);
        ilg.Emit(OpCodes.Box, methodParams[0]);
        MethodInfo writeLineObj = 
            typeof(Console).GetMethod("WriteLine", 
                new Type[] { typeof(object) });
        ilg.EmitCall(OpCodes.Call, writeLineObj, null);
        ilg.Emit(OpCodes.Ret);

        // Emit an entry point method; this must be in a
        // non-generic type.
        TypeBuilder dummy = myModule.DefineType("Dummy", 
            TypeAttributes.Class | TypeAttributes.NotPublic);
        MethodBuilder entryPoint = dummy.DefineMethod("Main", 
            MethodAttributes.Public | MethodAttributes.Static,
            null, null);
        ilg = entryPoint.GetILGenerator();
        // In order to call the static generic method GM, it is
        // necessary to create a constructed type from the 
        // generic type definition for Sample. This can be any
        // constructed type; in this case Sample<int> is used.
        Type SampleOfInt = 
            myType.MakeGenericType( typeof(int) );
        // Next get a MethodInfo representing the static generic
        // method GM on type Sample<int>.
        MethodInfo SampleOfIntGM = TypeBuilder.GetMethod(SampleOfInt, 
            genMethod);
        // Next get a MethodInfo for GM<string>, which is the
 
        // instantiation of GM that Main calls.
        MethodInfo GMOfString = 
            SampleOfIntGM.MakeGenericMethod( typeof(string) );
        // Finally, emit the call. Push a string onto
        // the stack, as the argument for the generic method.
        ilg.Emit(OpCodes.Ldstr, "Hello, world!");
        ilg.EmitCall(OpCodes.Call, GMOfString, null);
        ilg.Emit(OpCodes.Ret);

        myType.CreateType();
        dummy.CreateType();
        myAssembly.SetEntryPoint(entryPoint);
        myAssembly.Save(myAsmName.Name + ".exe");

        Console.WriteLine(myAsmName.Name + ".exe has been saved.");
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
MethodBuilder クラス
MethodBuilder メンバ
System.Reflection.Emit 名前空間
SetReturnType
SetParameters


このページでは「.NET Framework クラス ライブラリ リファレンス」からMethodBuilder.SetSignature メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からMethodBuilder.SetSignature メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からMethodBuilder.SetSignature メソッド を検索

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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2025 GRAS Group, Inc.RSS