Imports System
Imports System.Text
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices
Public Class Example
Public Shared Sub Main()
Dim asmName As New AssemblyName("YourAssemblyName")
Dim dynamicAsm As AssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName,AssemblyBuilderAccess.RunAndSave)
Dim dynamicMod As ModuleBuilder = dynamicAsm.DefineDynamicModule(asmName.Name, asmName.Name & ".dll")
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)
mb.SetImplementationFlags(mb.GetMethodImplementationFlags() Or MethodImplAttributes.PreserveSig)
Dim t As Type = tb.CreateType()
Dim mi As MethodInfo = t.GetMethod("GetTickCount")
Console.WriteLine(mi.Invoke(Nothing, Nothing))
Console.WriteLine("Saving: " & asmName.Name & ".dll")
dynamicAsm.Save(asmName.Name & ".dll")
End Sub
End Class
|