オーバーロードの一覧
間接的な呼び出しに対してアンマネージ呼び出し規約を指定して、MSIL (Microsoft Intermediate Language) ストリームに Calli 命令を書き込みます。
[Visual Basic] Overloads Public Sub EmitCalli(OpCode, CallingConvention, Type, Type())
[C#] public void EmitCalli(OpCode, CallingConvention, Type, Type[]);
[C++] public: void EmitCalli(OpCode, CallingConvention, Type*, Type[]);
[JScript] public function EmitCalli(OpCode, CallingConvention, Type, Type[]);
間接的な呼び出しに対してマネージ呼び出し規約を指定して、MSIL (Microsoft Intermediate Language) ストリームに Calli 命令を書き込みます。
[Visual Basic] Overloads Public Sub EmitCalli(OpCode, CallingConventions, Type, Type(), Type())
[C#] public void EmitCalli(OpCode, CallingConventions, Type, Type[], Type[]);
[C++] public: void EmitCalli(OpCode, CallingConventions, Type*, Type[], Type[]);
[JScript] public function EmitCalli(OpCode, CallingConventions, Type, Type[], Type[]);
使用例
[Visual Basic, C#, C++] 次のコード例は、 EmitCalli メソッドを使用して、動的なクラスの外部にあるアンマネージ型のメソッドを呼び出す方法を示しています。
[Visual Basic, C#, C++] メモ ここでは、EmitCalli のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("MyMethod", _
MethodAttributes.Public, _
returnType, mthdParamTypes)
' We will assume that an external unmanaged type "LegacyNumber" has been loaded, and
' that it has a method "ToString" which returns a string.
Dim unmanagedMthdMI As MethodInfo = Type.GetType("LegacyNumber").GetMethod("ToString")
Dim myMthdIL As ILGenerator = myMthdBuilder.GetILGenerator()
' Code to emit various IL opcodes here ...
' Load a reference to the specific object instance onto the stack.
myMthdIL.Emit(OpCodes.Ldc_I4, addrOfLegacyNumberObject)
myMthdIL.Emit(OpCodes.Ldobj, Type.GetType("LegacyNumber"))
' Make the call to the unmanaged type method, telling it that the method is
' the member of a specific instance, to expect a string
' as a return value, and that there are no explicit parameters.
myMthdIL.EmitCalli(OpCodes.Calli, System.Runtime.InteropServices.CallingConvention.ThisCall, _
GetType(String), New Type() {})
' More IL code emission here ...
[C#]
MethodBuilder myMthdBuilder = myTypeBuilder.DefineMethod("MyMethod",
MethodAttributes.Public,
returnType, mthdParamTypes);
// We will assume that an external unmanaged type "LegacyNumber" has been loaded, and
// that it has a method "ToString" which returns a string.
MethodInfo unmanagedMthdMI = Type.GetType("LegacyNumber").GetMethod("ToString");
ILGenerator myMthdIL = myMthdBuilder.GetILGenerator();
// Code to emit various IL opcodes here ...
// Load a reference to the specific object instance onto the stack.
myMthdIL.Emit(OpCodes.Ldc_I4, addrOfLegacyNumberObject);
myMthdIL.Emit(OpCodes.Ldobj, Type.GetType("LegacyNumber"));
// Make the call to the unmanaged type method, telling it that the method is
// the member of a specific instance, to expect a string
// as a return value, and that there are no explicit parameters.
myMthdIL.EmitCalli(OpCodes.Calli,
System.Runtime.InteropServices.CallingConvention.ThisCall,
typeof(string),
new Type[] {});
// More IL code emission here ...
[C++]
MethodBuilder* myMthdBuilder = myTypeBuilder->DefineMethod(S"MyMethod",
MethodAttributes::Public,
returnType, mthdParamTypes);
// We will assume that an external unmanaged type "LegacyNumber" has been loaded, and
// that it has a method "ToString" which returns a String.
MethodInfo* unmanagedMthdMI = Type::GetType(S"LegacyNumber")->GetMethod(S"ToString");
ILGenerator* myMthdIL = myMthdBuilder->GetILGenerator();
// Code to emit various IL opcodes here ...
// Load a reference to the specific Object instance onto the stack.
myMthdIL->Emit(OpCodes::Ldc_I4, addrOfLegacyNumberObject);
myMthdIL->Emit(OpCodes::Ldobj, Type::GetType(S"LegacyNumber"));
// Make the call to the unmanaged type method, telling it that the method is
// the member of a specific instance, to expect a String
// as a return value, and that there are no explicit parameters.
myMthdIL->EmitCalli(OpCodes::Calli,
System::Runtime::InteropServices::CallingConvention::ThisCall,
__typeof(String),
new Type*[0]);
// More IL code emission here ...
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
参照
ILGenerator クラス | ILGenerator メンバ | System.Reflection.Emit 名前空間