ModuleBuilder.DefinePInvokeMethod Metoda

Definice

Definuje PInvoke metodu.

Přetížení

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

Definuje PInvoke metodu se zadaným názvem, název knihovny DLL, ve které je definována metoda, atributy metody, konvence volání metody, návratový typ metody, typy parametrů metody a PInvoke příznaky...

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

Definuje metodu se zadaným názvem, název knihovny DLL, ve které je metoda definována, atributy metody, konvenci volání metody, návratový typ metody, typy parametrů metody a PInvoke PInvoke příznaky.

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

Definuje PInvoke metodu se zadaným názvem, název knihovny DLL, ve které je definována metoda, atributy metody, konvence volání metody, návratový typ metody, typy parametrů metody a PInvoke příznaky...

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);
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
Public Function DefinePInvokeMethod (name As String, dllName As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), nativeCallConv As CallingConvention, nativeCharSet As CharSet) As MethodBuilder

Parametry

name
String

Název PInvoke metody. name nelze obsahovat vložené hodnoty null.

dllName
String

Název knihovny DLL, ve které PInvoke je metoda definována.

attributes
MethodAttributes

Atributy metody

callingConvention
CallingConventions

Konvence volání metody.

returnType
Type

Návratový typ metody

parameterTypes
Type[]

Typy parametrů metody.

nativeCallConv
CallingConvention

Nativní konvence volání.

nativeCharSet
CharSet

Nativní znaková sada metody.

Návraty

MethodBuilder

Definovaná PInvoke metoda.

Výjimky

Metoda není statická nebo je-li obsažený typ rozhraní.

-nebo-

Metoda je abstraktní.

-nebo-

Metoda byla dříve definována.

name nebo dllName je null .

Nadřazený typ byl dříve vytvořen pomocí CreateType()

Příklady

následující příklad ukazuje použití DefinePInvokeMethod metody pro vytvoření MethodBuilder externí nespravované metody, MessageBoxA v rozhraní Windows API. V příkladu se zobrazí okno se zprávou s tlačítky Opakovat a Storno a v okně se zprávou se zobrazí návratová hodnota.

Důležité

Chcete-li získat nenulovou návratovou hodnotu, je nutné přidat MethodImplAttributes.PreserveSig do příznaků implementace metody po vytvoření MethodBuilder pomocí MethodBuilder.GetMethodImplementationFlags MethodBuilder.SetImplementationFlags metod a.

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

Poznámky

Některé atributy importu DLL (viz popis System. Runtime. InteropServices. DllImportAttribute) nejde zadat jako argumenty této metody. Takové atributy by měly být nastaveny vygenerováním vlastního atributu pro metodu. Například atribut import knihovny DLL PreserveSig je nastaven vygenerováním vlastního atributu.

Poznámka

počínaje verzí .NET Framework 2,0 Service Pack 1 již tento člen nepotřebuje ReflectionPermission s ReflectionPermissionFlag.ReflectionEmit příznakem. (Viz problémy se zabezpečením při generování reflexe.) chcete-li použít tuto funkci, vaše aplikace by měla cílit na .NET Framework 3,5 nebo novější.

Platí pro

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

Definuje metodu se zadaným názvem, název knihovny DLL, ve které je metoda definována, atributy metody, konvenci volání metody, návratový typ metody, typy parametrů metody a PInvoke PInvoke příznaky.

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);
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
Public Function DefinePInvokeMethod (name As String, dllName As String, entryName As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type(), nativeCallConv As CallingConvention, nativeCharSet As CharSet) As MethodBuilder

Parametry

name
String

Název PInvoke metody. name nesmí obsahovat vložené hodnoty null.

dllName
String

Název knihovny DLL, ve které je PInvoke definována metoda.

entryName
String

Název vstupního bodu v knihovně DLL.

attributes
MethodAttributes

Atributy metody.

callingConvention
CallingConventions

Konvence volání metody.

returnType
Type

Návratový typ metody

parameterTypes
Type[]

Typy parametrů metody.

nativeCallConv
CallingConvention

Konvence nativního volání.

nativeCharSet
CharSet

Nativní znaková sada metody.

Návraty

MethodBuilder

Definovaná PInvoke metoda.

Výjimky

Metoda není statická nebo pokud je obsahující typ rozhraní nebo pokud je metoda abstraktní metody , pokud byla dříve definována.

name nebo dllName je null .

Obsahující typ byl dříve vytvořen pomocí . CreateType()

Příklady

Následující příklad znázorňuje použití metody k vytvoření pro externí nespravovanou metodu v rozhraní DefinePInvokeMethod MethodBuilder WINDOWS MessageBoxA API. Příklad zobrazí okno se zprávou s tlačítky Opakovat a Zrušit a zobrazí návratovou hodnotu z okna se zprávou.

Důležité

Chcete-li získat nenulovou návratovou hodnotu, je nutné přidat do příznaků implementace metody po vytvoření , pomocí metod MethodImplAttributes.PreserveSig MethodBuilder a MethodBuilder.GetMethodImplementationFlags MethodBuilder.SetImplementationFlags .

Tento příklad používá jiné přetížení DefinePInvokeMethod metody, ale technika je stejná.

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

Poznámky

Některé atributy importu knihovny DLL (viz popis DllImportAttribute ) nelze zadat jako argumenty této metody. Tyto atributy by měly být nastaveny vypisováním vlastního atributu pro metodu . Například atribut importu knihovny DLL PreserveSig se nastaví vypisováním vlastního atributu.

Poznámka

Počínaje aktualizací .NET Framework 2.0 Service Pack 1 už tento člen s ReflectionPermission příznakem ReflectionPermissionFlag.ReflectionEmit nevyžaduje. (Viz Problémy se zabezpečením v reflexi vysílané.) Pokud chcete tuto funkci použít, měla by vaše aplikace cílit na .NET Framework 3.5 nebo novější.

Platí pro