ModuleBuilder.DefinePInvokeMethod ModuleBuilder.DefinePInvokeMethod ModuleBuilder.DefinePInvokeMethod Method

定義

PInvoke メソッドを定義します。Defines a PInvoke method.

オーバーロード

DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

メソッド名、メソッドが定義される DLL の名前、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメーターの型、および PInvoke フラグを指定して、PInvoke メソッドを定義します。Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

メソッド名、メソッドが定義される DLL の名前、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメーターの型、および PInvoke フラグを指定して、PInvoke メソッドを定義します。Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

メソッド名、メソッドが定義される DLL の名前、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメーターの型、および PInvoke フラグを指定して、PInvoke メソッドを定義します。Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

public:
 System::Reflection::Emit::MethodBuilder ^ DefinePInvokeMethod(System::String ^ name, System::String ^ dllName, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod (string name, string dllName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
member this.DefinePInvokeMethod : string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder

パラメーター

name
String String String

PInvoke メソッドの名前。The name of the PInvoke method. name に埋め込み null 値を含めることはできません。name cannot contain embedded nulls.

dllName
String String String

PInvoke メソッドが定義されている DLL の名前。The name of the DLL in which the PInvoke method is defined.

attributes
MethodAttributes MethodAttributes MethodAttributes

メソッドの属性。The attributes of the method.

callingConvention
CallingConventions CallingConventions CallingConventions

メソッドの呼び出し規則。The method's calling convention.

returnType
Type Type Type

メソッドの戻り値の型。The method's return type.

parameterTypes
Type[]

メソッドのパラメーター型。The types of the method's parameters.

nativeCallConv
CallingConvention CallingConvention CallingConvention

ネイティブ呼び出し規則。The native calling convention.

nativeCharSet
CharSet CharSet CharSet

メソッドのネイティブ文字セット。The method's native character set.

戻り値

定義された PInvoke メソッド。The defined PInvoke method.

例外

メソッドが静的でないか、格納している型がインターフェイスです。The method is not static or if the containing type is an interface.

または-or-

抽象メソッドです。The method is abstract.

または-or-

メソッドは以前に定義されています。The method was previously defined.

name または dllNamenull です。name or dllName is null.

含んでいる型が CreateType() を使用して以前に作成されています。The containing type has been previously created using CreateType()

次の例は、 DefinePInvokeMethodメソッドを使用して、Windows API で外部のアンマネージメソッドMethodBuilder MessageBoxAのを作成する方法を示しています。The following example illustrates the use of the DefinePInvokeMethod method to create a MethodBuilder for an external unmanaged method, MessageBoxA, in the Windows API. この例では、[再試行] ボタンと [キャンセル] ボタンを含むメッセージボックスを表示し、メッセージボックスの戻り値を表示します。The example displays a message box with Retry and Cancel buttons, and displays the return value from the message box.

重要

0以外の戻り値を取得するには、メソッドMethodImplAttributes.PreserveSig MethodBuilder.GetMethodImplementationFlagsMethodBuilder.SetImplementationFlagsメソッドを使用して、 MethodBuilderを作成した後に、メソッドの実装フラグにを追加する必要があります。To get a non-zero return value, you must add MethodImplAttributes.PreserveSig to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags methods.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

const int MB_RETRYCANCEL = 5;

void main()
{
   AssemblyName^ myAssemblyName = gcnew AssemblyName("TempAssembly");

   // Define a dynamic assembly in the current application domain.
   AssemblyBuilder^ myAssemblyBuilder = 
      AppDomain::CurrentDomain->DefineDynamicAssembly(
                  myAssemblyName, AssemblyBuilderAccess::Run);

   // Define a dynamic module in "TempAssembly" assembly.
   ModuleBuilder^ myModuleBuilder = 
      myAssemblyBuilder->DefineDynamicModule("TempModule");

   array<Type^>^ paramTypes = 
      { int::typeid, String::typeid, String::typeid, int::typeid };

   // Define a PInvoke method.
   MethodBuilder^ piMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
      "MessageBoxA",
      "user32.dll",
      MethodAttributes::Public | MethodAttributes::Static | MethodAttributes::PinvokeImpl,
      CallingConventions::Standard,
      int::typeid,
      paramTypes,
      CallingConvention::Winapi,
      CharSet::Ansi);

   // Add PreserveSig to the method implementation flags. NOTE: If this line
   // is commented out, the return value will be zero when the method is
   // invoked.
   piMethodBuilder->SetImplementationFlags(
      piMethodBuilder->GetMethodImplementationFlags() | MethodImplAttributes::PreserveSig);

   // Create global methods.
   myModuleBuilder->CreateGlobalFunctions();

   // Arguments for calling the method.
   array<Object^>^ arguments = 
      { (Object^)(int) 0, "Hello World", "Title", MB_RETRYCANCEL };

   MethodInfo^ pinvokeMethod = myModuleBuilder->GetMethod("MessageBoxA");
   Console::WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...");
   Console::WriteLine("Message box returned: {0}", 
      pinvokeMethod->Invoke(nullptr, arguments));
};


/* This code example produces input similar to the following:

Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
 */
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

namespace PInvoke
{
   public class Example
   {
      const int MB_RETRYCANCEL = 5;

      static void Main()
      {
         AssemblyName myAssemblyName = new AssemblyName("TempAssembly");

         // Define a dynamic assembly in the current application domain.
         AssemblyBuilder myAssemblyBuilder = 
            AppDomain.CurrentDomain.DefineDynamicAssembly(
                        myAssemblyName, AssemblyBuilderAccess.Run);

         // Define a dynamic module in "TempAssembly" assembly.
         ModuleBuilder myModuleBuilder = 
            myAssemblyBuilder.DefineDynamicModule("TempModule");

         Type[] paramTypes = { typeof(int), typeof(string), typeof(string), typeof(int) };

         // Define a PInvoke method.
         MethodBuilder piMethodBuilder = myModuleBuilder.DefinePInvokeMethod(
            "MessageBoxA",
            "user32.dll",
            MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
            CallingConventions.Standard,
            typeof(int),
            paramTypes,
            CallingConvention.Winapi,
            CharSet.Ansi);
         
         // Add PreserveSig to the method implementation flags. NOTE: If this line
         // is commented out, the return value will be zero when the method is
         // invoked.
         piMethodBuilder.SetImplementationFlags(
            piMethodBuilder.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

         // Create global methods.
         myModuleBuilder.CreateGlobalFunctions();

         // Arguments for calling the method.
         Object[] arguments = { 0, "Hello World", "Title", MB_RETRYCANCEL };

         MethodInfo pinvokeMethod = myModuleBuilder.GetMethod("MessageBoxA");
         Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...");
         Console.WriteLine("Message box returned: {0}", 
            pinvokeMethod.Invoke(null, arguments));
      }
   }
}

/* This code example produces input similar to the following:

Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
 */
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Namespace PInvoke

   Public Class Example
   
      Const MB_RETRYCANCEL As Integer = 5

      Shared Sub Main()
      
         Dim myAssemblyName As New AssemblyName("TempAssembly")

         ' Define a dynamic assembly in the current application domain.
         Dim myAssemblyBuilder As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                        myAssemblyName, AssemblyBuilderAccess.Run)

         ' Define a dynamic module in "TempAssembly" assembly.
         Dim myModuleBuilder As ModuleBuilder = _
            myAssemblyBuilder.DefineDynamicModule("TempModule")

         Dim paramTypes() As Type = _
            { GetType(Integer), GetType(string), GetType(string), GetType(Integer) }

         ' Define a PInvoke method.
         Dim piMethodBuilder As MethodBuilder = myModuleBuilder.DefinePInvokeMethod( _
            "MessageBoxA", _
            "user32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            paramTypes, _
            CallingConvention.Winapi, _
            CharSet.Ansi)
         
         ' Add PreserveSig to the method implementation flags. NOTE: If this line
         ' is commented out, the return value will be zero when the method is
         ' invoked.
         piMethodBuilder.SetImplementationFlags( _
            piMethodBuilder.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)

         ' Create global methods.
         myModuleBuilder.CreateGlobalFunctions()

         ' Arguments for calling the method.
         Dim arguments() As Object= { 0, "Hello World", "Title", MB_RETRYCANCEL }

         Dim pinvokeMethod As MethodInfo = _
            myModuleBuilder.GetMethod("MessageBoxA")
         Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...")
         Console.WriteLine("Message box returned: {0}", _
            pinvokeMethod.Invoke(Nothing, arguments))

      End Sub
   End Class
End Namespace

' This code example produces input similar to the following:
'
'Testing module-level PInvoke method created with DefinePInvokeMethod...
'Message box returned: 4

注釈

一部の DLL インポート属性 (DllImportAttribute の説明を参照) は、このメソッドの引数として指定することはできません。Some DLL import attributes (see the description of System.Runtime.InteropServices.DllImportAttribute) cannot be specified as arguments to this method. このような属性は、メソッドのカスタム属性を生成することによって設定する必要があります。Such attributes should be set by emitting a custom attribute for the method. たとえば、DLL インポート属性PreserveSigは、カスタム属性を出力することによって設定されます。For example, the DLL import attribute PreserveSig is set by emitting a custom attribute.

注意

以降では.NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1、このメンバーはフラグをReflectionPermission使用するReflectionPermissionFlag.ReflectionEmit必要がなくなりました。Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (「リフレクション出力のセキュリティの問題」を参照してください)。この機能を使用するには、アプリケーションで .NET Framework 3.5.NET Framework 3.5 以降を対象とする必要があります。(See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5.NET Framework 3.5 or later.

DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)

メソッド名、メソッドが定義される DLL の名前、メソッドの属性、メソッドの呼び出し規約、メソッドの戻り値の型、メソッドのパラメーターの型、および PInvoke フラグを指定して、PInvoke メソッドを定義します。Defines a PInvoke method with the specified name, the name of the DLL in which the method is defined, the attributes of the method, the calling convention of the method, the return type of the method, the types of the parameters of the method, and the PInvoke flags.

public:
 System::Reflection::Emit::MethodBuilder ^ DefinePInvokeMethod(System::String ^ name, System::String ^ dllName, System::String ^ entryName, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod (string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
member this.DefinePInvokeMethod : string * string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder

パラメーター

name
String String String

PInvoke メソッドの名前。The name of the PInvoke method. name に埋め込み null 値を含めることはできません。name cannot contain embedded nulls.

dllName
String String String

PInvoke メソッドが定義されている DLL の名前。The name of the DLL in which the PInvoke method is defined.

entryName
String String String

DLL 内のエントリ ポイントの名前。The name of the entry point in the DLL.

attributes
MethodAttributes MethodAttributes MethodAttributes

メソッドの属性。The attributes of the method.

callingConvention
CallingConventions CallingConventions CallingConventions

メソッドの呼び出し規則。The method's calling convention.

returnType
Type Type Type

メソッドの戻り値の型。The method's return type.

parameterTypes
Type[]

メソッドのパラメーター型。The types of the method's parameters.

nativeCallConv
CallingConvention CallingConvention CallingConvention

ネイティブ呼び出し規則。The native calling convention.

nativeCharSet
CharSet CharSet CharSet

メソッドのネイティブ文字セット。The method's native character set.

戻り値

定義された PInvoke メソッド。The defined PInvoke method.

例外

メソッドが静的でないか、格納している型がインターフェイスです。または、メソッドが既に定義されている場合は、メソッドが抽象メソッドです。The method is not static or if the containing type is an interface or if the method is abstract of if the method was previously defined.

name または dllNamenull です。name or dllName is null.

含んでいる型が CreateType() を使用して以前に作成されています。The containing type has been previously created using CreateType()

次の例は、 DefinePInvokeMethodメソッドを使用して、Windows API で外部のアンマネージメソッドMethodBuilder MessageBoxAのを作成する方法を示しています。The following example illustrates the use of the DefinePInvokeMethod method to create a MethodBuilder for an external unmanaged method, MessageBoxA, in the Windows API. この例では、[再試行] ボタンと [キャンセル] ボタンを含むメッセージボックスを表示し、メッセージボックスの戻り値を表示します。The example displays a message box with Retry and Cancel buttons, and displays the return value from the message box.

重要

0以外の戻り値を取得するには、メソッドMethodImplAttributes.PreserveSig MethodBuilder.GetMethodImplementationFlagsMethodBuilder.SetImplementationFlagsメソッドを使用して、 MethodBuilderを作成した後に、メソッドの実装フラグにを追加する必要があります。To get a non-zero return value, you must add MethodImplAttributes.PreserveSig to the method implementation flags after you create the MethodBuilder, by using the MethodBuilder.GetMethodImplementationFlags and MethodBuilder.SetImplementationFlags methods.

この例では、 DefinePInvokeMethodメソッドの異なるオーバーロードを使用していますが、その手法は同じです。This example uses a different overload of the DefinePInvokeMethod method, but the technique is the same.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

const int MB_RETRYCANCEL = 5;

void main()
{
   AssemblyName^ myAssemblyName = gcnew AssemblyName("TempAssembly");

   // Define a dynamic assembly in the current application domain.
   AssemblyBuilder^ myAssemblyBuilder = 
      AppDomain::CurrentDomain->DefineDynamicAssembly(
                  myAssemblyName, AssemblyBuilderAccess::Run);

   // Define a dynamic module in "TempAssembly" assembly.
   ModuleBuilder^ myModuleBuilder = 
      myAssemblyBuilder->DefineDynamicModule("TempModule");

   array<Type^>^ paramTypes = 
      { int::typeid, String::typeid, String::typeid, int::typeid };

   // Define a PInvoke method.
   MethodBuilder^ piMethodBuilder = myModuleBuilder->DefinePInvokeMethod(
      "MessageBoxA",
      "user32.dll",
      MethodAttributes::Public | MethodAttributes::Static | MethodAttributes::PinvokeImpl,
      CallingConventions::Standard,
      int::typeid,
      paramTypes,
      CallingConvention::Winapi,
      CharSet::Ansi);

   // Add PreserveSig to the method implementation flags. NOTE: If this line
   // is commented out, the return value will be zero when the method is
   // invoked.
   piMethodBuilder->SetImplementationFlags(
      piMethodBuilder->GetMethodImplementationFlags() | MethodImplAttributes::PreserveSig);

   // Create global methods.
   myModuleBuilder->CreateGlobalFunctions();

   // Arguments for calling the method.
   array<Object^>^ arguments = 
      { (Object^)(int) 0, "Hello World", "Title", MB_RETRYCANCEL };

   MethodInfo^ pinvokeMethod = myModuleBuilder->GetMethod("MessageBoxA");
   Console::WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...");
   Console::WriteLine("Message box returned: {0}", 
      pinvokeMethod->Invoke(nullptr, arguments));
};


/* This code example produces input similar to the following:

Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
 */
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

namespace PInvoke
{
   public class Example
   {
      const int MB_RETRYCANCEL = 5;

      static void Main()
      {
         AssemblyName myAssemblyName = new AssemblyName("TempAssembly");

         // Define a dynamic assembly in the current application domain.
         AssemblyBuilder myAssemblyBuilder = 
            AppDomain.CurrentDomain.DefineDynamicAssembly(
                        myAssemblyName, AssemblyBuilderAccess.Run);

         // Define a dynamic module in "TempAssembly" assembly.
         ModuleBuilder myModuleBuilder = 
            myAssemblyBuilder.DefineDynamicModule("TempModule");

         Type[] paramTypes = { typeof(int), typeof(string), typeof(string), typeof(int) };

         // Define a PInvoke method.
         MethodBuilder piMethodBuilder = myModuleBuilder.DefinePInvokeMethod(
            "MessageBoxA",
            "user32.dll",
            MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
            CallingConventions.Standard,
            typeof(int),
            paramTypes,
            CallingConvention.Winapi,
            CharSet.Ansi);
         
         // Add PreserveSig to the method implementation flags. NOTE: If this line
         // is commented out, the return value will be zero when the method is
         // invoked.
         piMethodBuilder.SetImplementationFlags(
            piMethodBuilder.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);

         // Create global methods.
         myModuleBuilder.CreateGlobalFunctions();

         // Arguments for calling the method.
         Object[] arguments = { 0, "Hello World", "Title", MB_RETRYCANCEL };

         MethodInfo pinvokeMethod = myModuleBuilder.GetMethod("MessageBoxA");
         Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...");
         Console.WriteLine("Message box returned: {0}", 
            pinvokeMethod.Invoke(null, arguments));
      }
   }
}

/* This code example produces input similar to the following:

Testing module-level PInvoke method created with DefinePInvokeMethod...
Message box returned: 4
 */
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Namespace PInvoke

   Public Class Example
   
      Const MB_RETRYCANCEL As Integer = 5

      Shared Sub Main()
      
         Dim myAssemblyName As New AssemblyName("TempAssembly")

         ' Define a dynamic assembly in the current application domain.
         Dim myAssemblyBuilder As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                        myAssemblyName, AssemblyBuilderAccess.Run)

         ' Define a dynamic module in "TempAssembly" assembly.
         Dim myModuleBuilder As ModuleBuilder = _
            myAssemblyBuilder.DefineDynamicModule("TempModule")

         Dim paramTypes() As Type = _
            { GetType(Integer), GetType(string), GetType(string), GetType(Integer) }

         ' Define a PInvoke method.
         Dim piMethodBuilder As MethodBuilder = myModuleBuilder.DefinePInvokeMethod( _
            "MessageBoxA", _
            "user32.dll", _
            MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
            CallingConventions.Standard, _
            GetType(Integer), _
            paramTypes, _
            CallingConvention.Winapi, _
            CharSet.Ansi)
         
         ' Add PreserveSig to the method implementation flags. NOTE: If this line
         ' is commented out, the return value will be zero when the method is
         ' invoked.
         piMethodBuilder.SetImplementationFlags( _
            piMethodBuilder.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)

         ' Create global methods.
         myModuleBuilder.CreateGlobalFunctions()

         ' Arguments for calling the method.
         Dim arguments() As Object= { 0, "Hello World", "Title", MB_RETRYCANCEL }

         Dim pinvokeMethod As MethodInfo = _
            myModuleBuilder.GetMethod("MessageBoxA")
         Console.WriteLine("Testing module-level PInvoke method created with DefinePInvokeMethod...")
         Console.WriteLine("Message box returned: {0}", _
            pinvokeMethod.Invoke(Nothing, arguments))

      End Sub
   End Class
End Namespace

' This code example produces input similar to the following:
'
'Testing module-level PInvoke method created with DefinePInvokeMethod...
'Message box returned: 4

注釈

一部の DLL インポート属性 (のDllImportAttribute説明を参照) を、このメソッドの引数として指定することはできません。Some DLL import attributes (see the description of DllImportAttribute) cannot be specified as arguments to this method. このような属性は、メソッドのカスタム属性を生成することによって設定する必要があります。Such attributes should be set by emitting a custom attribute for the method. たとえば、DLL インポート属性PreserveSigは、カスタム属性を出力することによって設定されます。For example, the DLL import attribute PreserveSig is set by emitting a custom attribute.

注意

以降では.NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1、このメンバーはフラグをReflectionPermission使用するReflectionPermissionFlag.ReflectionEmit必要がなくなりました。Starting with the .NET Framework 2.0 Service Pack 1.NET Framework 2.0 Service Pack 1, this member no longer requires ReflectionPermission with the ReflectionPermissionFlag.ReflectionEmit flag. (「リフレクション出力のセキュリティの問題」を参照してください)。この機能を使用するには、アプリケーションで .NET Framework 3.5.NET Framework 3.5 以降を対象とする必要があります。(See Security Issues in Reflection Emit.) To use this functionality, your application should target the .NET Framework 3.5.NET Framework 3.5 or later.

適用対象