TypeBuilder.DefinePInvokeMethod Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Bir PInvoke yöntem tanımlar.
Aşırı Yüklemeler
| Name | Description |
|---|---|
| DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Adı, yönteminin tanımlandığı DLL'nin adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Adı, yöntemin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet) |
Adı, yönteminin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametre türleri, |
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
Adı, yönteminin tanımlandığı DLL'nin adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve PInvoke bayraklar verilen bir PInvoke yöntemi tanımlar.
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
Parametreler
- name
- String
yönteminin PInvoke adı.
name katıştırılmış null içeremez.
- dllName
- String
Yönteminin tanımlandığı DLL'nin PInvoke adı.
- attributes
- MethodAttributes
yönteminin öznitelikleri.
- callingConvention
- CallingConventions
Yöntemin çağırma kuralı.
- returnType
- Type
Yöntemin dönüş türü.
- parameterTypes
- Type[]
Yöntemin parametrelerinin türleri.
- nativeCallConv
- CallingConvention
Yerel çağırma kuralı.
- nativeCharSet
- CharSet
Yöntemin yerel karakter kümesi.
Döndürülenler
Tanımlanan PInvoke yöntem.
- Öznitelikler
Özel durumlar
Yöntemi statik değil.
-veya-
Üst tür bir arabirimdir.
-veya-
Yöntemi soyut.
-veya-
Yöntemi daha önce tanımlanmıştı.
-veya-
name veya dllName uzunluğu sıfırdır.
name veya dllName şeklindedir null.
İçeren tür daha önce kullanılarak CreateType()oluşturulmuştur.
Örnekler
Aşağıdaki örnekte, yöntemini kullanarak DefinePInvokeMethod bir PInvoke yöntem oluşturma ve ve yöntemlerini kullanarak yöntemini oluşturduktan MethodImplAttributes.PreserveSigsonra yöntemi uygulama bayraklarına bayrağının MethodBuilderMethodBuilder.GetMethodImplementationFlags nasıl ekleneceği MethodBuilder.SetImplementationFlags gösterilmektedir.
Important
Sıfır olmayan bir dönüş değeri almak için bayrağını MethodImplAttributes.PreserveSig eklemeniz gerekir.
Örnek, yöntemini içeren tek bir dinamik modüle ve tek bir türe MyTypePInvoke sahip bir dinamik derleme oluşturur. yöntemi Win32 PInvokeGetTickCount işlevini temsil eder.
Örnek çalıştırıldığında yöntemini yürütür PInvoke . Ayrıca dinamik derlemeyi PInvokeTest.dllolarak kaydeder.
sınıfını ve içerdiği MyType (Visual Basic'de static) Shared yöntemini incelemek için PInvoke kullanabilirsiniz. statik MyType.GetTickCount yöntemini kullanan bir Visual Basic veya C# programı derlemek için csc.exe veya vbc.exeçalıştırdığınızda DLL'ye başvuru ekleyebilirsiniz; örneğin, /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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (açıklamasına DllImportAttributebakın) bu yöntemin bağımsız değişkenleri olarak belirtilemez. Örneğin, yöntem bir değer döndürüyorsa, yöntem oluşturulduktan sonra MethodImplAttributes.PreserveSig DLL içeri aktarma özniteliği PInvoke eklenmelidir. Örnekte bunun nasıl yapıldığını gösterilmektedir.
Şunlara uygulanır
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
Adı, yöntemin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametrelerinin türleri ve PInvoke bayraklar verilen bir PInvoke yöntemi tanımlar.
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
Parametreler
- name
- String
yönteminin PInvoke adı.
name katıştırılmış null içeremez.
- dllName
- String
Yönteminin tanımlandığı DLL'nin PInvoke adı.
- entryName
- String
DLL'deki giriş noktasının adı.
- attributes
- MethodAttributes
yönteminin öznitelikleri.
- callingConvention
- CallingConventions
Yöntemin çağırma kuralı.
- returnType
- Type
Yöntemin dönüş türü.
- parameterTypes
- Type[]
Yöntemin parametrelerinin türleri.
- nativeCallConv
- CallingConvention
Yerel çağırma kuralı.
- nativeCharSet
- CharSet
Yöntemin yerel karakter kümesi.
Döndürülenler
Tanımlanan PInvoke yöntem.
- Öznitelikler
Özel durumlar
Yöntemi statik değil.
-veya-
Üst tür bir arabirimdir.
-veya-
Yöntemi soyut.
-veya-
Yöntemi daha önce tanımlanmıştı.
-veya-
, nameveya dllName uzunluğu entryNamesıfırdır.
name, dllNameveya entryName şeklindedir null.
İçeren tür daha önce kullanılarak CreateType()oluşturulmuştur.
Örnekler
Aşağıdaki kod örneği, yöntemini kullanarak DefinePInvokeMethod bir PInvoke yöntem oluşturmayı ve ve yöntemlerini kullanarak yöntemini oluşturduktan MethodImplAttributes.PreserveSigsonra yöntemi uygulama bayraklarına bayrağının MethodBuilderMethodBuilder.GetMethodImplementationFlags nasıl ekleneceğini MethodBuilder.SetImplementationFlags gösterir.
Important
Sıfır olmayan bir dönüş değeri almak için bayrağını MethodImplAttributes.PreserveSig eklemeniz gerekir.
Örnek, yöntemini içeren tek bir dinamik modüle ve tek bir türe MyTypePInvoke sahip bir dinamik derleme oluşturur. yöntemi Win32 PInvokeGetTickCount işlevini temsil eder.
Örnek çalıştırıldığında yöntemini yürütür PInvoke . Ayrıca dinamik derlemeyi PInvokeTest.dllolarak kaydeder.
sınıfını ve içerdiği MyType (Visual Basic'de static) Shared yöntemini incelemek için PInvoke kullanabilirsiniz. statik MyType.GetTickCount yöntemini kullanan bir Visual Basic veya C# programı derlemek için csc.exe veya vbc.exeçalıştırdığınızda DLL'ye başvuru ekleyebilirsiniz; örneğin, /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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (açıklamasına DllImportAttributebakın) bu yöntemin bağımsız değişkenleri olarak belirtilemez. Örneğin, yöntem bir değer döndürüyorsa, yöntem oluşturulduktan sonra MethodImplAttributes.PreserveSig DLL içeri aktarma özniteliği PInvoke eklenmelidir. Örnekte bunun nasıl yapıldığını gösterilmektedir.
Şunlara uygulanır
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet)
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
- Kaynak:
- TypeBuilder.cs
Adı, yönteminin tanımlandığı DLL'nin adı, giriş noktasının adı, yöntemin öznitelikleri, yöntemin çağırma kuralı, yöntemin dönüş türü, yöntemin parametre türleri, PInvoke bayraklar ve parametreler ve dönüş türü için özel değiştiriciler verilen bir PInvoke yöntemi tanımlar.
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
Parametreler
- name
- String
yönteminin PInvoke adı.
name katıştırılmış null içeremez.
- dllName
- String
Yönteminin tanımlandığı DLL'nin PInvoke adı.
- entryName
- String
DLL'deki giriş noktasının adı.
- attributes
- MethodAttributes
yönteminin öznitelikleri.
- callingConvention
- CallingConventions
Yöntemin çağırma kuralı.
- returnType
- Type
Yöntemin dönüş türü.
- returnTypeRequiredCustomModifiers
- Type[]
yönteminin dönüş türü için gerekli özel değiştiricileri IsConsttemsil eden bir tür dizisi. Dönüş türünde gerekli özel değiştirici yoksa belirtin null.
- returnTypeOptionalCustomModifiers
- Type[]
yönteminin dönüş türü için gibi isteğe bağlı özel değiştiricileri IsConsttemsil eden bir tür dizisi. Dönüş türünde isteğe bağlı özel değiştirici yoksa belirtin null.
- parameterTypes
- Type[]
Yöntemin parametrelerinin türleri.
- parameterTypeRequiredCustomModifiers
- Type[][]
Tür dizileri dizisi. Her tür dizisi, gibi IsConstilgili parametre için gerekli özel değiştiricileri temsil eder. Belirli bir parametrenin gerekli özel değiştiricisi yoksa, bir tür dizisi yerine belirtin null . Parametrelerden hiçbiri için özel değiştirici gerekli değilse, dizi dizisi yerine belirtin null .
- parameterTypeOptionalCustomModifiers
- Type[][]
Tür dizileri dizisi. Her tür dizisi, IsConstgibi ilgili parametre için isteğe bağlı özel değiştiricileri temsil eder. Belirli bir parametrenin isteğe bağlı özel değiştiricisi yoksa, tür dizisi yerine belirtin null . Parametrelerin hiçbiri isteğe bağlı özel değiştiricilere sahip değilse, dizi dizisi yerine belirtin null .
- nativeCallConv
- CallingConvention
Yerel çağırma kuralı.
- nativeCharSet
- CharSet
Yöntemin yerel karakter kümesi.
Döndürülenler
MethodBuilder Tanımlanan PInvoke yöntemi temsil eden bir.
- Öznitelikler
Özel durumlar
Yöntemi statik değil.
-veya-
Üst tür bir arabirimdir.
-veya-
Yöntemi soyut.
-veya-
Yöntemi daha önce tanımlanmıştı.
-veya-
, nameveya dllName uzunluğu entryNamesıfırdır.
-veya-
veya parameterTypeRequiredCustomModifiers boyutuparameterTypeOptionalCustomModifiers, boyutuna parameterTypeseşit değildir.
name, dllNameveya entryName şeklindedir null.
Türü daha önce kullanılarak CreateType()oluşturulmuştur.
-veya-
Geçerli dinamik tür IsGenericType için özelliği olur true, ancak IsGenericTypeDefinition özelliği olur false.
Örnekler
Aşağıdaki kod örneği, yöntemini kullanarak DefinePInvokeMethod bir PInvoke yöntem oluşturmayı ve ve yöntemlerini kullanarak yöntemini oluşturduktan MethodImplAttributes.PreserveSigsonra yöntemi uygulama bayraklarına bayrağının MethodBuilderMethodBuilder.GetMethodImplementationFlags nasıl ekleneceğini MethodBuilder.SetImplementationFlags gösterir.
Örnek, yöntemini içeren tek bir dinamik modüle ve tek bir türe MyTypePInvoke sahip bir dinamik derleme oluşturur. yöntemi Win32 PInvokeGetTickCount işlevini temsil eder.
Important
Sıfır olmayan bir dönüş değeri almak için bayrağını MethodImplAttributes.PreserveSig eklemeniz gerekir.
Note
Örnek, özel değiştiriciler belirtmeyen bir aşırı yükleme kullanır. Özel değiştiricileri belirtmek için örnek kodu bu yöntem aşırı yüklemesini kullanacak şekilde değiştirin.
Örnek çalıştırıldığında yöntemini yürütür PInvoke . Ayrıca dinamik derlemeyi PInvokeTest.dllolarak kaydeder.
sınıfını ve içerdiği MyType (Visual Basic'de static) Shared yöntemini incelemek için PInvoke kullanabilirsiniz. statik MyType.GetTickCount yöntemini kullanan bir Visual Basic veya C# programı derlemek için csc.exe veya vbc.exeçalıştırdığınızda DLL'ye başvuru ekleyebilirsiniz; örneğin, /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
Açıklamalar
Bazı DLL içeri aktarma öznitelikleri (açıklamasına DllImportAttributebakın) bu yöntemin bağımsız değişkenleri olarak belirtilemez. Örneğin, yöntem bir değer döndürüyorsa, yöntem oluşturulduktan sonra MethodImplAttributes.PreserveSig DLL içeri aktarma özniteliği PInvoke eklenmelidir. Örnekte bunun nasıl yapıldığını gösterilmektedir.
Note
Özel değiştiriciler hakkında daha fazla bilgi için bkz. ECMA C# ve Ortak Dil Altyapısı Standartları ve Standart ECMA-335 - Ortak Dil Altyapısı (CLI).