TypeBuilder.DefinePInvokeMethod Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Definuje metodu PInvoke .
Přetížení
| Name | Description |
|---|---|
| DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Definuje metodu s daným |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Definuje metodu s daným |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet) |
Definuje metodu s daným |
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
Definuje metodu s daným PInvoke názvem, název knihovny DLL, ve které je metoda definována, atributy metody, volání konvence 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);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
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);
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);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")>]
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
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 nemůže 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
PInvoke Definovaná metoda.
- Atributy
Výjimky
Metoda není statická.
nebo
Nadřazený typ je rozhraní.
nebo
Metoda je abstraktní.
nebo
Metoda byla dříve definována.
nebo
Délka name nebo dllName je nula.
name nebo dllName je null.
Typ obsahující byl dříve vytvořen pomocí CreateType().
Příklady
Následující příklad ukazuje, jak použít metodu DefinePInvokeMethodPInvoke k vytvoření metody a jak přidat MethodImplAttributes.PreserveSig příznak do příznaků implementace metody po vytvoření MethodBuilder, pomocí MethodBuilder.GetMethodImplementationFlags a MethodBuilder.SetImplementationFlags metody.
Important
Pokud chcete získat nenulovou návratovou hodnotu, musíte přidat MethodImplAttributes.PreserveSig příznak.
Příklad vytvoří dynamické sestavení s jedním dynamickým modulem a jedním typem, MyTypekterý obsahuje metodu PInvoke . Metoda PInvoke představuje funkci Win32 GetTickCount .
Když je příklad spuštěn, spustí metodu PInvoke . Uloží také dynamické sestavení jako PInvokeTest.dll. Pomocí Ildasm.exe (IL Disassembler) můžete prozkoumat třídu MyType a static (Shared v Visual Basic) PInvoke metoda, kterou obsahuje. Můžete zkompilovat Visual Basic nebo program jazyka C#, který používá statickou metodu MyType.GetTickCount zahrnutím odkazu na knihovnu DLL při spuštění csc.exe nebo vbc.exe; například /r:PInvokeTest.dll.
using System;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
public class Example
{
public static void Main()
{
// Create the AssemblyBuilder.
AssemblyName asmName = new AssemblyName("PInvokeTest");
AssemblyBuilder dynamicAsm = AppDomain.CurrentDomain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndSave
);
// Create the module.
ModuleBuilder dynamicMod =
dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll");
// Create the TypeBuilder for the class that will contain the
// signature for the PInvoke call.
TypeBuilder tb = dynamicMod.DefineType(
"MyType",
TypeAttributes.Public | TypeAttributes.UnicodeClass
);
MethodBuilder mb = tb.DefinePInvokeMethod(
"GetTickCount",
"Kernel32.dll",
MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
typeof(int),
Type.EmptyTypes,
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.
mb.SetImplementationFlags(
mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);
// The PInvoke method does not have a method body.
// Create the class and test the method.
Type t = tb.CreateType();
MethodInfo mi = t.GetMethod("GetTickCount");
Console.WriteLine("Testing PInvoke method...");
Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(null, null));
// Produce the .dll file.
Console.WriteLine("Saving: " + asmName.Name + ".dll");
dynamicAsm.Save(asmName.Name + ".dll");
}
}
/* This example produces output similar to the following:
Testing PInvoke method...
GetTickCount returned: 1312576235
Saving: PInvokeTest.dll
*/
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices
Public Class Example
Public Shared Sub Main()
' Create the AssemblyBuilder.
Dim asmName As New AssemblyName("PInvokeTest")
Dim dynamicAsm As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
AssemblyBuilderAccess.RunAndSave)
' Create the module.
Dim dynamicMod As ModuleBuilder = _
dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")
' Create the TypeBuilder for the class that will contain the
' signature for the PInvoke call.
Dim tb As TypeBuilder = dynamicMod.DefineType("MyType", _
TypeAttributes.Public Or TypeAttributes.UnicodeClass)
Dim mb As MethodBuilder = tb.DefinePInvokeMethod( _
"GetTickCount", _
"Kernel32.dll", _
MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
CallingConventions.Standard, _
GetType(Integer), _
Type.EmptyTypes, _
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.
mb.SetImplementationFlags( _
mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
' The PInvoke method does not have a method body.
' Create the class and test the method.
Dim t As Type = tb.CreateType()
Dim mi As MethodInfo = t.GetMethod("GetTickCount")
Console.WriteLine("Testing PInvoke method...")
Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(Nothing, Nothing))
' Produce the .dll file.
Console.WriteLine("Saving: " & asmName.Name & ".dll")
dynamicAsm.Save(asmName.Name & ".dll")
End Sub
End Class
' This example produces output similar to the following:
'
'Testing PInvoke method...
'GetTickCount returned: 1313078714
'Saving: PInvokeTest.dll
Poznámky
Některé atributy importu knihovny DLL (viz popis DllImportAttribute) nelze zadat jako argumenty této metody. Například atribut MethodImplAttributes.PreserveSig import knihovny DLL musí být přidán po PInvoke vytvoření metody, pokud metoda vrátí hodnotu. Příklad ukazuje, jak to udělat.
Platí pro
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
Definuje metodu s daným PInvoke názvem, název knihovny DLL, ve které je metoda definována, název vstupního bodu, 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::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);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
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);
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);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")>]
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
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 nemůže obsahovat vložené hodnoty null.
- dllName
- String
Název knihovny DLL, ve které PInvoke je metoda definována.
- 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
Nativní konvence volání.
- nativeCharSet
- CharSet
Nativní znaková sada metody.
Návraty
PInvoke Definovaná metoda.
- Atributy
Výjimky
Metoda není statická.
nebo
Nadřazený typ je rozhraní.
nebo
Metoda je abstraktní.
nebo
Metoda byla dříve definována.
nebo
Délka name, dllNamenebo entryName je nula.
name, dllNamenebo entryName je null.
Typ obsahující byl dříve vytvořen pomocí CreateType().
Příklady
Následující příklad kódu ukazuje, jak použít metodu DefinePInvokeMethodPInvoke k vytvoření metody a jak přidat MethodImplAttributes.PreserveSig příznak do příznaky implementace metody po vytvoření MethodBuilder, pomocí MethodBuilder.GetMethodImplementationFlags a MethodBuilder.SetImplementationFlags metody.
Important
Pokud chcete získat nenulovou návratovou hodnotu, musíte přidat MethodImplAttributes.PreserveSig příznak.
Příklad vytvoří dynamické sestavení s jedním dynamickým modulem a jedním typem, MyTypekterý obsahuje metodu PInvoke . Metoda PInvoke představuje funkci Win32 GetTickCount .
Když je příklad spuštěn, spustí metodu PInvoke . Uloží také dynamické sestavení jako PInvokeTest.dll. Pomocí Ildasm.exe (IL Disassembler) můžete prozkoumat třídu MyType a static (Shared v Visual Basic) PInvoke metoda, kterou obsahuje. Můžete zkompilovat Visual Basic nebo program jazyka C#, který používá statickou metodu MyType.GetTickCount zahrnutím odkazu na knihovnu DLL při spuštění csc.exe nebo vbc.exe; například /r:PInvokeTest.dll.
using System;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
public class Example
{
public static void Main()
{
// Create the AssemblyBuilder.
AssemblyName asmName = new AssemblyName("PInvokeTest");
AssemblyBuilder dynamicAsm = AppDomain.CurrentDomain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndSave
);
// Create the module.
ModuleBuilder dynamicMod =
dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll");
// Create the TypeBuilder for the class that will contain the
// signature for the PInvoke call.
TypeBuilder tb = dynamicMod.DefineType(
"MyType",
TypeAttributes.Public | TypeAttributes.UnicodeClass
);
MethodBuilder mb = tb.DefinePInvokeMethod(
"GetTickCount",
"Kernel32.dll",
MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
typeof(int),
Type.EmptyTypes,
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.
mb.SetImplementationFlags(
mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);
// The PInvoke method does not have a method body.
// Create the class and test the method.
Type t = tb.CreateType();
MethodInfo mi = t.GetMethod("GetTickCount");
Console.WriteLine("Testing PInvoke method...");
Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(null, null));
// Produce the .dll file.
Console.WriteLine("Saving: " + asmName.Name + ".dll");
dynamicAsm.Save(asmName.Name + ".dll");
}
}
/* This example produces output similar to the following:
Testing PInvoke method...
GetTickCount returned: 1312576235
Saving: PInvokeTest.dll
*/
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices
Public Class Example
Public Shared Sub Main()
' Create the AssemblyBuilder.
Dim asmName As New AssemblyName("PInvokeTest")
Dim dynamicAsm As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
AssemblyBuilderAccess.RunAndSave)
' Create the module.
Dim dynamicMod As ModuleBuilder = _
dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")
' Create the TypeBuilder for the class that will contain the
' signature for the PInvoke call.
Dim tb As TypeBuilder = dynamicMod.DefineType("MyType", _
TypeAttributes.Public Or TypeAttributes.UnicodeClass)
Dim mb As MethodBuilder = tb.DefinePInvokeMethod( _
"GetTickCount", _
"Kernel32.dll", _
MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
CallingConventions.Standard, _
GetType(Integer), _
Type.EmptyTypes, _
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.
mb.SetImplementationFlags( _
mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
' The PInvoke method does not have a method body.
' Create the class and test the method.
Dim t As Type = tb.CreateType()
Dim mi As MethodInfo = t.GetMethod("GetTickCount")
Console.WriteLine("Testing PInvoke method...")
Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(Nothing, Nothing))
' Produce the .dll file.
Console.WriteLine("Saving: " & asmName.Name & ".dll")
dynamicAsm.Save(asmName.Name & ".dll")
End Sub
End Class
' This example produces output similar to the following:
'
'Testing PInvoke method...
'GetTickCount returned: 1313078714
'Saving: PInvokeTest.dll
Poznámky
Některé atributy importu knihovny DLL (viz popis DllImportAttribute) nelze zadat jako argumenty této metody. Například atribut MethodImplAttributes.PreserveSig import knihovny DLL musí být přidán po PInvoke vytvoření metody, pokud metoda vrátí hodnotu. Příklad ukazuje, jak to udělat.
Platí pro
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet)
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
- Zdroj:
- TypeBuilder.cs
Definuje metodu s daným PInvoke názvem, název knihovny DLL, ve které je metoda definována, název vstupního bodu, atributy metody, konvence volání metody, návratový typ metody, typy parametrů metody, PInvoke příznaky a vlastní modifikátory parametrů a návratový typ.
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 ^> ^ returnTypeRequiredCustomModifiers, cli::array <Type ^> ^ returnTypeOptionalCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ parameterTypeRequiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ parameterTypeOptionalCustomModifiers, System::Runtime::InteropServices::CallingConvention nativeCallConv, System::Runtime::InteropServices::CharSet nativeCharSet);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")]
public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers, 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[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers, Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers, 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[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("P/Invoke marshalling may dynamically access members that could be trimmed.")>]
member this.DefinePInvokeMethod : string * string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] * System.Runtime.InteropServices.CallingConvention * System.Runtime.InteropServices.CharSet -> System.Reflection.Emit.MethodBuilder
member this.DefinePInvokeMethod : string * string * string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * 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, returnTypeRequiredCustomModifiers As Type(), returnTypeOptionalCustomModifiers As Type(), parameterTypes As Type(), parameterTypeRequiredCustomModifiers As Type()(), parameterTypeOptionalCustomModifiers As Type()(), nativeCallConv As CallingConvention, nativeCharSet As CharSet) As MethodBuilder
Parametry
- name
- String
Název PInvoke metody.
name nemůže obsahovat vložené hodnoty null.
- dllName
- String
Název knihovny DLL, ve které PInvoke je metoda definována.
- 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.
- returnTypeRequiredCustomModifiers
- Type[]
Pole typů představujících požadované vlastní modifikátory, například IsConst, pro návratový typ metody. Pokud návratový typ neobsahuje žádné požadované vlastní modifikátory, zadejte null.
- returnTypeOptionalCustomModifiers
- Type[]
Pole typů představujících volitelné vlastní modifikátory, například IsConst, pro návratový typ metody. Pokud návratový typ neobsahuje žádné volitelné vlastní modifikátory, zadejte null.
- parameterTypes
- Type[]
Typy parametrů metody.
- parameterTypeRequiredCustomModifiers
- Type[][]
Pole polí typů. Každé pole typů představuje požadované vlastní modifikátory pro odpovídající parametr, například IsConst. Pokud určitý parametr nemá žádné požadované vlastní modifikátory, zadejte null místo pole typů. Pokud žádný z parametrů nevyžaduje vlastní modifikátory, zadejte null místo pole polí.
- parameterTypeOptionalCustomModifiers
- Type[][]
Pole polí typů. Každé pole typů představuje volitelné vlastní modifikátory pro odpovídající parametr, například IsConst. Pokud konkrétní parametr nemá žádné volitelné vlastní modifikátory, zadejte null místo pole typů. Pokud žádný z parametrů nemá volitelné vlastní modifikátory, zadejte null místo pole polí.
- nativeCallConv
- CallingConvention
Nativní konvence volání.
- nativeCharSet
- CharSet
Nativní znaková sada metody.
Návraty
A MethodBuilder představující definovanou PInvoke metodu.
- Atributy
Výjimky
Metoda není statická.
nebo
Nadřazený typ je rozhraní.
nebo
Metoda je abstraktní.
nebo
Metoda byla dříve definována.
nebo
Délka name, dllNamenebo entryName je nula.
nebo
Velikost parameterTypeRequiredCustomModifiers nebo parameterTypeOptionalCustomModifiers se nerovná velikosti parameterTypes.
name, dllNamenebo entryName je null.
Typ byl dříve vytvořen pomocí CreateType().
nebo
U aktuálního dynamického typu IsGenericType je truevlastnost , ale vlastnost IsGenericTypeDefinition je false.
Příklady
Následující příklad kódu ukazuje, jak použít metodu DefinePInvokeMethodPInvoke k vytvoření metody a jak přidat MethodImplAttributes.PreserveSig příznak do příznaky implementace metody po vytvoření MethodBuilder, pomocí MethodBuilder.GetMethodImplementationFlags a MethodBuilder.SetImplementationFlags metody.
Příklad vytvoří dynamické sestavení s jedním dynamickým modulem a jedním typem, MyTypekterý obsahuje metodu PInvoke . Metoda PInvoke představuje funkci Win32 GetTickCount .
Important
Pokud chcete získat nenulovou návratovou hodnotu, musíte přidat MethodImplAttributes.PreserveSig příznak.
Note
Příklad používá přetížení, které nezadá vlastní modifikátory. Chcete-li určit vlastní modifikátory, změňte ukázkový kód tak, aby místo toho používal přetížení této metody.
Když je příklad spuštěn, spustí metodu PInvoke . Uloží také dynamické sestavení jako PInvokeTest.dll. Pomocí Ildasm.exe (IL Disassembler) můžete prozkoumat třídu MyType a static (Shared v Visual Basic) PInvoke metoda, kterou obsahuje. Můžete zkompilovat Visual Basic nebo program jazyka C#, který používá statickou metodu MyType.GetTickCount zahrnutím odkazu na knihovnu DLL při spuštění csc.exe nebo vbc.exe; například /r:PInvokeTest.dll.
using System;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
public class Example
{
public static void Main()
{
// Create the AssemblyBuilder.
AssemblyName asmName = new AssemblyName("PInvokeTest");
AssemblyBuilder dynamicAsm = AppDomain.CurrentDomain.DefineDynamicAssembly(
asmName,
AssemblyBuilderAccess.RunAndSave
);
// Create the module.
ModuleBuilder dynamicMod =
dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll");
// Create the TypeBuilder for the class that will contain the
// signature for the PInvoke call.
TypeBuilder tb = dynamicMod.DefineType(
"MyType",
TypeAttributes.Public | TypeAttributes.UnicodeClass
);
MethodBuilder mb = tb.DefinePInvokeMethod(
"GetTickCount",
"Kernel32.dll",
MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl,
CallingConventions.Standard,
typeof(int),
Type.EmptyTypes,
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.
mb.SetImplementationFlags(
mb.GetMethodImplementationFlags() | MethodImplAttributes.PreserveSig);
// The PInvoke method does not have a method body.
// Create the class and test the method.
Type t = tb.CreateType();
MethodInfo mi = t.GetMethod("GetTickCount");
Console.WriteLine("Testing PInvoke method...");
Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(null, null));
// Produce the .dll file.
Console.WriteLine("Saving: " + asmName.Name + ".dll");
dynamicAsm.Save(asmName.Name + ".dll");
}
}
/* This example produces output similar to the following:
Testing PInvoke method...
GetTickCount returned: 1312576235
Saving: PInvokeTest.dll
*/
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices
Public Class Example
Public Shared Sub Main()
' Create the AssemblyBuilder.
Dim asmName As New AssemblyName("PInvokeTest")
Dim dynamicAsm As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, _
AssemblyBuilderAccess.RunAndSave)
' Create the module.
Dim dynamicMod As ModuleBuilder = _
dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")
' Create the TypeBuilder for the class that will contain the
' signature for the PInvoke call.
Dim tb As TypeBuilder = dynamicMod.DefineType("MyType", _
TypeAttributes.Public Or TypeAttributes.UnicodeClass)
Dim mb As MethodBuilder = tb.DefinePInvokeMethod( _
"GetTickCount", _
"Kernel32.dll", _
MethodAttributes.Public Or MethodAttributes.Static Or MethodAttributes.PinvokeImpl, _
CallingConventions.Standard, _
GetType(Integer), _
Type.EmptyTypes, _
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.
mb.SetImplementationFlags( _
mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
' The PInvoke method does not have a method body.
' Create the class and test the method.
Dim t As Type = tb.CreateType()
Dim mi As MethodInfo = t.GetMethod("GetTickCount")
Console.WriteLine("Testing PInvoke method...")
Console.WriteLine("GetTickCount returned: {0}", mi.Invoke(Nothing, Nothing))
' Produce the .dll file.
Console.WriteLine("Saving: " & asmName.Name & ".dll")
dynamicAsm.Save(asmName.Name & ".dll")
End Sub
End Class
' This example produces output similar to the following:
'
'Testing PInvoke method...
'GetTickCount returned: 1313078714
'Saving: PInvokeTest.dll
Poznámky
Některé atributy importu knihovny DLL (viz popis DllImportAttribute) nelze zadat jako argumenty této metody. Například atribut MethodImplAttributes.PreserveSig import knihovny DLL musí být přidán po PInvoke vytvoření metody, pokud metoda vrátí hodnotu. Příklad ukazuje, jak to udělat.
Note
Další informace o vlastních modifikátorech najdete v tématu ECMA C# a standardECMA-335 – Common Language Infrastructure (CLI).