次の方法で共有


ILGenerator.EmitCalli メソッド (OpCode, CallingConvention, Type, Type )

間接的な呼び出しに対してアンマネージ呼び出し規約を指定して、MSIL (Microsoft Intermediate Language) ストリームに Calli 命令を書き込みます。

Overloads Public Sub EmitCalli( _
   ByVal opcode As OpCode, _   ByVal unmanagedCallConv As CallingConvention, _   ByVal returnType As Type, _   ByVal parameterTypes() As Type _)
[C#]
public void EmitCalli(OpCodeopcode,CallingConventionunmanagedCallConv,TypereturnType,Type[] parameterTypes);
[C++]
public: void EmitCalli(OpCodeopcode,CallingConventionunmanagedCallConv,Type* returnType,Type* parameterTypes[]);
[JScript]
public function EmitCalli(
   opcode : OpCode,unmanagedCallConv : CallingConvention,returnType : Type,parameterTypes : Type[]);

パラメータ

  • opcode
    ストリームに書き込む MSIL 命令。
  • unmanagedCallConv
    使用するアンマネージ呼び出し規約。
  • returnType
    結果の Type
  • parameterTypes
    命令に必要な引数の型。

解説

Calli 命令をストリームに書き込むには、 EmitCalli を使用します。 Emit は使用しないでください。

使用例

[Visual Basic, C#, C++] 次のコード例は、 EmitCalli メソッドを使用して、動的なクラスの外部にあるアンマネージ型のメソッドを呼び出す方法を示しています。

 
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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

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

参照

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