次の方法で共有


TypeBuilder.DefineMethod メソッド (String, MethodAttributes, CallingConventions, Type, Type )

名前とメソッド シグネチャを指定して、新しいメソッドをクラスに追加します。

Overloads Public Function DefineMethod( _
   ByVal name As String, _   ByVal attributes As MethodAttributes, _   ByVal callingConvention As CallingConventions, _   ByVal returnType As Type, _   ByVal parameterTypes() As Type _) As MethodBuilder
[C#]
public MethodBuilder DefineMethod(stringname,MethodAttributesattributes,CallingConventionscallingConvention,TypereturnType,Type[] parameterTypes);
[C++]
public: MethodBuilder* DefineMethod(String* name,MethodAttributesattributes,CallingConventionscallingConvention,Type* returnType,Type* parameterTypes[]);
[JScript]
public function DefineMethod(
   name : String,attributes : MethodAttributes,callingConvention : CallingConventions,returnType : Type,parameterTypes : Type[]) : MethodBuilder;

パラメータ

  • name
    メソッドの名前。name に null を埋め込むことはできません。
  • attributes
    メソッドの属性。
  • callingConvention
    メソッドの呼び出し規約。
  • returnType
    メソッドの戻り値の型。
  • parameterTypes
    メソッドのパラメータの型。

戻り値

定義されたメソッド。

例外

例外の種類 条件
ArgumentException name の長さが 0 です。

または

このメソッドの親の型がインターフェイスで、このメソッドが仮想メソッドではありません。

ArgumentNullException name が null 参照 (Visual Basic では Nothing) です。
InvalidOperationException この型は、 CreateType を使用して既に作成されています。

使用例

[Visual Basic, C#, C++] 次のコード例は、 DefineMethod を使用して、コンストラクタの固有のシグネチャと属性を動的な型に対して設定し、それに対応する MSIL に書き込みを行うための MethodBuilder を返す方法を示しています。

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


Public Interface IMyInterface
   Function HelloMethod(parameter As String) As String
End Interface 'IMyInterface

Public Class EmittedClass
   Public Shared Sub Main()
      Dim myNestedClassType As Type = CreateCallee(Thread.GetDomain())
      ' Create an instance of 'MyNestedClass'.
      Dim myInterface As IMyInterface = _
            CType(Activator.CreateInstance(myNestedClassType), IMyInterface)
      Console.WriteLine(myInterface.HelloMethod("Bill"))
   End Sub 'Main

   ' Create the callee transient dynamic assembly.
   Private Shared Function CreateCallee(myAppDomain As AppDomain) As Type
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "EmittedClass"
      ' Create the callee dynamic assembly.
      Dim myAssembly As AssemblyBuilder = _
               myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
      ' Create a dynamic module in the callee assembly.
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Define a public class named "MyHelloWorld".
      Dim myHelloWorldType As TypeBuilder = _
               myModule.DefineType("MyHelloWorld", TypeAttributes.Public)
      ' Define a public nested class named 'MyNestedClass'.
      Dim myNestedClassType As TypeBuilder = _
               myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
               GetType(EmittedClass), New Type() {GetType(IMyInterface)})
      ' Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))
      ' Define 'HelloMethod' of 'IMyInterface'.
      Dim myHelloMethod As MethodBuilder = _
               myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
               MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})
      ' Generate IL for 'GetGreeting' method.
      Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ")
      myMethodIL.Emit(OpCodes.Ldarg_1)
      Dim infoMethod As MethodInfo = _
               GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
      myMethodIL.Emit(OpCodes.Call, infoMethod)
      myMethodIL.Emit(OpCodes.Ret)

      Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
      ' Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)
      ' Create 'MyHelloWorld' type.
      Dim myType As Type = myHelloWorldType.CreateType()
      ' Create 'MyNestedClass' type.
      Return myNestedClassType.CreateType()
   End Function 'CreateCallee
End Class 'EmittedClass

[C#] 
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;


public interface IMyInterface
{
   String HelloMethod(String parameter);
}

public class EmittedClass
{
   public static void Main()
   {
      Type myNestedClassType = CreateCallee(Thread.GetDomain());
      // Cretae an instance of 'MyNestedClass'.
      IMyInterface myInterface =
         (IMyInterface)Activator.CreateInstance(myNestedClassType);
      Console.WriteLine(myInterface.HelloMethod("Bill"));
   }

   // Create the callee transient dynamic assembly.
   private static Type CreateCallee(AppDomain myAppDomain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedClass";
      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly =
         myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
      // Create a dynamic module in the callee assembly.
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "MyHelloWorld".
      TypeBuilder myHelloWorldType =
         myModule.DefineType("MyHelloWorld", TypeAttributes.Public);
      // Define a public nested class named 'MyNestedClass'.
      TypeBuilder myNestedClassType =
         myHelloWorldType.DefineNestedType("MyNestedClass",
            TypeAttributes.NestedPublic, typeof(EmittedClass),
            new Type[]{typeof(IMyInterface)});
      // Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));
      // Define 'HelloMethod' of 'IMyInterface'.
      MethodBuilder myHelloMethod =
         myNestedClassType.DefineMethod("HelloMethod",
            MethodAttributes.Public | MethodAttributes.Virtual,
            typeof(String), new Type[]{typeof(String)});
      // Generate IL for 'GetGreeting' method.
      ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ");
      myMethodIL.Emit(OpCodes.Ldarg_1);
      MethodInfo infoMethod =
         typeof(String).GetMethod("Concat",new Type[]{typeof(string),typeof(string)});
      myMethodIL.Emit(OpCodes.Call, infoMethod);
      myMethodIL.Emit(OpCodes.Ret);

      MethodInfo myHelloMethodInfo =
         typeof(IMyInterface).GetMethod("HelloMethod");
      // Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
      // Create 'MyHelloWorld' type.
      Type myType = myHelloWorldType.CreateType();
      // Create 'MyNestedClass' type.
      return myNestedClassType.CreateType();
   }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;


public __gc __interface IMyInterface
{
    String* HelloMethod(String* parameter);
};

public __gc class EmittedClass
{
public:
    static void Main()
    {
        Type* myNestedClassType = CreateCallee(Thread::GetDomain());
        // Create an instance of 'MyNestedClass'.
        IMyInterface* myInterface =
            dynamic_cast<IMyInterface*>(Activator::CreateInstance(myNestedClassType));
        Console::WriteLine(myInterface->HelloMethod(S"Bill"));
    }

    // Create the callee transient dynamic assembly.
private:
    static Type* CreateCallee(AppDomain* myAppDomain)
    {
        AssemblyName* myAssemblyName = new AssemblyName();
        myAssemblyName->Name = S"EmittedClass";
        // Create the callee dynamic assembly.
        AssemblyBuilder* myAssembly =
            myAppDomain->DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess::Run);
        // Create a dynamic module in the callee assembly.
        ModuleBuilder* myModule = myAssembly->DefineDynamicModule(S"EmittedModule");
        // Define a public class named "MyHelloWorld".
        TypeBuilder* myHelloWorldType =
            myModule->DefineType(S"MyHelloWorld", TypeAttributes::Public);
        // Define a public nested class named 'MyNestedClass'.
        Type* temp0 [] = {__typeof(IMyInterface)};
        TypeBuilder* myNestedClassType =
            myHelloWorldType->DefineNestedType(S"MyNestedClass",
            TypeAttributes::NestedPublic, __typeof(EmittedClass),
            temp0);
        // Implement 'IMyInterface' interface.
        myNestedClassType->AddInterfaceImplementation(__typeof(IMyInterface));
        // Define 'HelloMethod' of 'IMyInterface'.
        Type* temp1 [] = {__typeof(String)};
        MethodBuilder* myHelloMethod =
            myNestedClassType->DefineMethod(S"HelloMethod",
                static_cast<MethodAttributes>(MethodAttributes::Public | MethodAttributes::Virtual),
            __typeof(String), temp1);
        // Generate IL for 'GetGreeting' method.
        ILGenerator* myMethodIL = myHelloMethod->GetILGenerator();
        myMethodIL->Emit(OpCodes::Ldstr, S"Hi! ");
        myMethodIL->Emit(OpCodes::Ldarg_1);
        Type* temp2 [] = {__typeof(String),__typeof(String)};
        MethodInfo* infoMethod =
            __typeof(String)->GetMethod(S"Concat",temp2);
        myMethodIL->Emit(OpCodes::Call, infoMethod);
        myMethodIL->Emit(OpCodes::Ret);

        MethodInfo* myHelloMethodInfo =
            __typeof(IMyInterface)->GetMethod(S"HelloMethod");
        // Implement 'HelloMethod' of 'IMyInterface'.
        myNestedClassType->DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
        // Create 'MyHelloWorld' type.
        Type* myType = myHelloWorldType->CreateType();
        // Create 'MyNestedClass' type.
        return myNestedClassType->CreateType();
    }
};

int main()
{
    EmittedClass::Main();
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

TypeBuilder クラス | TypeBuilder メンバ | System.Reflection.Emit 名前空間 | TypeBuilder.DefineMethod オーバーロードの一覧