TypeBuilder.DefinePInvokeMethod Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
PInvoke Mendefinisikan metode.
Overload
| DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Mendefinisikan metode yang |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet) |
Mendefinisikan metode yang |
| DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet) |
Mendefinisikan metode yang |
DefinePInvokeMethod(String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
Mendefinisikan metode yang PInvoke diberi namanya, nama DLL tempat metode ditentukan, atribut metode, konvensi pemanggilan metode, jenis pengembalian metode, jenis parameter metode, dan PInvoke bendera.
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
Parameter
- name
- String
Nama PInvoke metode .
name tidak boleh berisi null yang disematkan.
- dllName
- String
Nama DLL tempat PInvoke metode ditentukan.
- attributes
- MethodAttributes
Atribut metode .
- callingConvention
- CallingConventions
Konvensi panggilan metode.
- returnType
- Type
Jenis pengembalian metode.
- parameterTypes
- Type[]
Jenis parameter metode.
- nativeCallConv
- CallingConvention
Konvensi panggilan asli.
- nativeCharSet
- CharSet
Set karakter asli metode.
Mengembalikan
Metode yang ditentukan PInvoke .
Pengecualian
Metode ini tidak statis.
-atau-
Jenis induk adalah antarmuka.
-atau-
Metode ini abstrak.
-atau-
Metode ini sebelumnya didefinisikan.
-atau-
Panjang name atau dllName adalah nol.
name atau dllName adalah null.
Jenis yang berisi telah dibuat sebelumnya menggunakan CreateType().
Contoh
Contoh berikut menunjukkan cara menggunakan DefinePInvokeMethod metode untuk membuat PInvoke metode, dan cara menambahkan MethodImplAttributes.PreserveSig bendera ke bendera implementasi metode setelah Anda membuat MethodBuilder, dengan menggunakan MethodBuilder.GetMethodImplementationFlags metode dan MethodBuilder.SetImplementationFlags .
Penting
Untuk mendapatkan nilai pengembalian bukan nol, Anda harus menambahkan MethodImplAttributes.PreserveSig bendera.
Contohnya membuat rakitan dinamis dengan satu modul dinamis dan satu jenis, MyType, yang berisi PInvoke metode . Metode ini PInvoke mewakili fungsi Win32 GetTickCount .
Ketika contoh dijalankan, ia menjalankan PInvoke metode . Ini juga menyimpan rakitan dinamis sebagai PInvokeTest.dll. Anda dapat menggunakan Ildasm.exe (IL Disassembler) untuk memeriksa MyType kelas dan static metode (Shared dalam Visual Basic) PInvoke yang dikandungnya. Anda dapat mengkompilasi program Visual Basic atau C# yang menggunakan metode statis MyType.GetTickCount dengan menyertakan referensi ke DLL saat Anda menjalankan csc.exe atau vbc.exe; misalnya, /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
Keterangan
Beberapa atribut impor DLL (lihat deskripsi DllImportAttribute) tidak dapat ditentukan sebagai argumen untuk metode ini. Misalnya, atribut MethodImplAttributes.PreserveSig impor DLL harus ditambahkan setelah PInvoke metode dibuat, jika metode mengembalikan nilai. Contoh menunjukkan cara melakukan ini.
Berlaku untuk
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], CallingConvention, CharSet)
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
Mendefinisikan metode yang PInvoke diberi namanya, nama DLL tempat metode ditentukan, nama titik masuk, atribut metode, konvensi pemanggilan metode, jenis pengembalian metode, jenis parameter metode, dan PInvoke bendera.
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
Parameter
- name
- String
Nama PInvoke metode .
name tidak boleh berisi null yang disematkan.
- dllName
- String
Nama DLL tempat PInvoke metode ditentukan.
- entryName
- String
Nama titik entri di DLL.
- attributes
- MethodAttributes
Atribut metode .
- callingConvention
- CallingConventions
Konvensi panggilan metode.
- returnType
- Type
Jenis pengembalian metode.
- parameterTypes
- Type[]
Jenis parameter metode.
- nativeCallConv
- CallingConvention
Konvensi panggilan asli.
- nativeCharSet
- CharSet
Set karakter asli metode.
Mengembalikan
Metode yang ditentukan PInvoke .
Pengecualian
Metode ini tidak statis.
-atau-
Jenis induk adalah antarmuka.
-atau-
Metode ini abstrak.
-atau-
Metode ini sebelumnya didefinisikan.
-atau-
Panjang name, dllName, atau entryName adalah nol.
name, dllName, atau entryName adalah null.
Jenis yang berisi telah dibuat sebelumnya menggunakan CreateType().
Contoh
Contoh kode berikut menunjukkan cara menggunakan DefinePInvokeMethod metode untuk membuat PInvoke metode, dan cara menambahkan MethodImplAttributes.PreserveSig bendera ke bendera implementasi metode setelah Anda membuat MethodBuilder, dengan menggunakan MethodBuilder.GetMethodImplementationFlags metode dan MethodBuilder.SetImplementationFlags .
Penting
Untuk mendapatkan nilai pengembalian bukan nol, Anda harus menambahkan MethodImplAttributes.PreserveSig bendera.
Contohnya membuat rakitan dinamis dengan satu modul dinamis dan satu jenis, MyType, yang berisi PInvoke metode . Metode ini PInvoke mewakili fungsi Win32 GetTickCount .
Ketika contoh dijalankan, ia menjalankan PInvoke metode . Ini juga menyimpan rakitan dinamis sebagai PInvokeTest.dll. Anda dapat menggunakan Ildasm.exe (IL Disassembler) untuk memeriksa MyType kelas dan static metode (Shared dalam Visual Basic) PInvoke yang dikandungnya. Anda dapat mengkompilasi program Visual Basic atau C# yang menggunakan metode statis MyType.GetTickCount dengan menyertakan referensi ke DLL saat Anda menjalankan csc.exe atau vbc.exe; misalnya, /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
Keterangan
Beberapa atribut impor DLL (lihat deskripsi DllImportAttribute) tidak dapat ditentukan sebagai argumen untuk metode ini. Misalnya, atribut MethodImplAttributes.PreserveSig impor DLL harus ditambahkan setelah PInvoke metode dibuat, jika metode mengembalikan nilai. Contoh menunjukkan cara melakukan ini.
Berlaku untuk
DefinePInvokeMethod(String, String, String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][], CallingConvention, CharSet)
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
- Sumber:
- TypeBuilder.cs
Mendefinisikan metode yang PInvoke diberi namanya, nama DLL tempat metode ditentukan, nama titik masuk, atribut metode, konvensi panggilan metode, jenis pengembalian metode, jenis parameter metode, bendera, PInvoke dan pengubah kustom untuk parameter dan jenis pengembalian.
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);
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);
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
Parameter
- name
- String
Nama PInvoke metode .
name tidak boleh berisi null yang disematkan.
- dllName
- String
Nama DLL tempat PInvoke metode ditentukan.
- entryName
- String
Nama titik entri di DLL.
- attributes
- MethodAttributes
Atribut metode .
- callingConvention
- CallingConventions
Konvensi panggilan metode.
- returnType
- Type
Jenis pengembalian metode.
- returnTypeRequiredCustomModifiers
- Type[]
Array jenis yang mewakili pengubah kustom yang diperlukan, seperti IsConst, untuk jenis pengembalian metode. Jika jenis pengembalian tidak memiliki pengubah kustom yang diperlukan, tentukan null.
- returnTypeOptionalCustomModifiers
- Type[]
Array jenis yang mewakili pengubah kustom opsional, seperti IsConst, untuk jenis pengembalian metode. Jika jenis pengembalian tidak memiliki pengubah kustom opsional, tentukan null.
- parameterTypes
- Type[]
Jenis parameter metode.
- parameterTypeRequiredCustomModifiers
- Type[][]
Array jenis array. Setiap array jenis mewakili pengubah kustom yang diperlukan untuk parameter yang sesuai, seperti IsConst. Jika parameter tertentu tidak memiliki pengubah kustom yang diperlukan, tentukan null alih-alih array jenis. Jika tidak ada parameter yang memerlukan pengubah kustom, tentukan null alih-alih array array.
- parameterTypeOptionalCustomModifiers
- Type[][]
Array jenis array. Setiap array jenis mewakili pengubah kustom opsional untuk parameter yang sesuai, seperti IsConst. Jika parameter tertentu tidak memiliki pengubah kustom opsional, tentukan null alih-alih array jenis. Jika tidak ada parameter yang memiliki pengubah kustom opsional, tentukan null alih-alih array array.
- nativeCallConv
- CallingConvention
Konvensi panggilan asli.
- nativeCharSet
- CharSet
Set karakter asli metode.
Mengembalikan
MethodBuilder mewakili metode yang ditentukanPInvoke.
Pengecualian
Metode ini tidak statis.
-atau-
Jenis induk adalah antarmuka.
-atau-
Metode ini abstrak.
-atau-
Metode ini sebelumnya didefinisikan.
-atau-
Panjang name, dllName, atau entryName adalah nol.
-atau-
Ukuran parameterTypeRequiredCustomModifiers atau parameterTypeOptionalCustomModifiers tidak sama dengan ukuran parameterTypes.
name, dllName, atau entryName adalah null.
Jenis sebelumnya dibuat menggunakan CreateType().
-atau-
Untuk jenis dinamis saat ini, IsGenericType properti adalah true, tetapi IsGenericTypeDefinition propertinya adalah false.
Contoh
Contoh kode berikut menunjukkan cara menggunakan DefinePInvokeMethod metode untuk membuat PInvoke metode, dan cara menambahkan MethodImplAttributes.PreserveSig bendera ke bendera implementasi metode setelah Anda membuat MethodBuilder, dengan menggunakan MethodBuilder.GetMethodImplementationFlags metode dan MethodBuilder.SetImplementationFlags .
Contohnya membuat rakitan dinamis dengan satu modul dinamis dan satu jenis, MyType, yang berisi PInvoke metode . Metode ini PInvoke mewakili fungsi Win32 GetTickCount .
Penting
Untuk mendapatkan nilai pengembalian bukan nol, Anda harus menambahkan MethodImplAttributes.PreserveSig bendera.
Catatan
Contoh menggunakan kelebihan beban yang tidak menentukan pengubah kustom. Untuk menentukan pengubah kustom, ubah kode contoh untuk menggunakan metode ini sebagai gantinya.
Ketika contoh dijalankan, ia menjalankan PInvoke metode . Ini juga menyimpan rakitan dinamis sebagai PInvokeTest.dll. Anda dapat menggunakan Ildasm.exe (IL Disassembler) untuk memeriksa MyType kelas dan static metode (Shared dalam Visual Basic) PInvoke yang dikandungnya. Anda dapat mengkompilasi program Visual Basic atau C# yang menggunakan metode statis MyType.GetTickCount dengan menyertakan referensi ke DLL saat Anda menjalankan csc.exe atau vbc.exe; misalnya, /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
Keterangan
Beberapa atribut impor DLL (lihat deskripsi DllImportAttribute) tidak dapat ditentukan sebagai argumen untuk metode ini. Misalnya, atribut MethodImplAttributes.PreserveSig impor DLL harus ditambahkan setelah PInvoke metode dibuat, jika metode mengembalikan nilai. Contoh menunjukkan cara melakukan ini.
Catatan
Untuk informasi selengkapnya tentang pengubah kustom, lihat ECMA C# dan Standar Infrastruktur Bahasa Umum dan ECMA-335 Standar - Infrastruktur Bahasa Umum (CLI).