TypeBuilder.DefineMethod メソッド
メソッドを定義します。
オーバーロードの一覧
名前とメソッド シグネチャを指定して、新しいメソッドをクラスに追加します。
[Visual Basic] Overloads Public Function DefineMethod(String, MethodAttributes, Type, Type()) As MethodBuilder
[C#] public MethodBuilder DefineMethod(string, MethodAttributes, Type, Type[]);
[C++] public: MethodBuilder* DefineMethod(String*, MethodAttributes, Type*, Type[]);
[JScript] public function DefineMethod(String, MethodAttributes, Type, Type[]) : MethodBuilder;
名前とメソッド シグネチャを指定して、新しいメソッドをクラスに追加します。
[Visual Basic] Overloads Public Function DefineMethod(String, MethodAttributes, CallingConventions, Type, Type()) As MethodBuilder
[C#] public MethodBuilder DefineMethod(string, MethodAttributes, CallingConventions, Type, Type[]);
[C++] public: MethodBuilder* DefineMethod(String*, MethodAttributes, CallingConventions, Type*, Type[]);
使用例
[Visual Basic, C#, C++] 次のコード例は、 DefineMethod を使用して、コンストラクタの固有のシグネチャと属性を動的な型に対して設定し、それに対応する MSIL に書き込みを行うための MethodBuilder を返す方法を示しています。
[Visual Basic, C#, C++] メモ ここでは、DefineMethod のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
参照
TypeBuilder クラス | TypeBuilder メンバ | System.Reflection.Emit 名前空間