-
March 4th, 2013, 09:00 AM
#1
Reflection.emit dll
Hi guys ,
first of all thank you for your great forum!
I have been reading your threads for long now, and I decided to register in order to ask for your kind help.
I would like to make a dll "on the fly" using vb.net project and il generator Opcode...but I am not good enough...
Would you please help me to make this dll :
Code:
Public Function makeit(ByVal nomeris As String, ByVal nome As String)
Dim rm As New Resources.ResourceManager(nomeris, Assembly.GetEntryAssembly)
Dim rb As Byte() = DirectCast(rm.GetObject(nome), Byte())
Return rb
End Function
What I need is something like this:
Code:
Public Class Form3
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DynamicJumpTableDemo.Main()
End Sub
End Class
Class DynamicJumpTableDemo
Public Overridable Overloads Sub Emit( _
ByVal opcode As OpCode _
)
End Sub
Shared myAsmName As New AssemblyName()
Public Shared Function BuildMyType() As Type
Dim myDomain As AppDomain = Thread.GetDomain()
myAsmName.Name = "MyDynamicAssembly"
Dim myAsmBuilder As AssemblyBuilder = myDomain.DefineDynamicAssembly(myAsmName, _
AssemblyBuilderAccess.RunAndSave)
Dim myModBuilder As ModuleBuilder = myAsmBuilder.DefineDynamicModule("JumpTableDemo", "mia.dll")
Dim myTypeBuilder As TypeBuilder = myModBuilder.DefineType("JumpTableDemo", _
TypeAttributes.Public Or TypeAttributes.Class)
Dim myMthdBuilder As MethodBuilder = myTypeBuilder.DefineMethod("SwitchMe", _
MethodAttributes.Public Or MethodAttributes.Static, _
GetType(String), New Type() {GetType(Integer)})
Dim myIL As ILGenerator = myMthdBuilder.GetILGenerator()
Dim defaultCase As Label = myIL.DefineLabel()
Dim endOfMethod As Label = myIL.DefineLabel()
Dim jumpTable() As Label = {myIL.DefineLabel(), _
myIL.DefineLabel(), _
myIL.DefineLabel(), _
myIL.DefineLabel(), _
myIL.DefineLabel()}
myIL.Emit(OpCodes.Ldarg_0)
myIL.Emit(OpCodes.Switch, jumpTable)
myIL.Emit(OpCodes.Call, GetType(System.Reflection.Assembly).GetMethod("GetEntryAssembly"))
myIL.Emit(OpCodes.Br_S, defaultCase)
myIL.MarkLabel(jumpTable(0))
myIL.Emit(OpCodes.Ldstr, "are no bananas")
myIL.Emit(OpCodes.Br_S, endOfMethod)
myIL.MarkLabel(jumpTable(1))
myIL.Emit(OpCodes.Ldstr, "is one banana")
myIL.Emit(OpCodes.Br_S, endOfMethod)
myIL.MarkLabel(jumpTable(2))
myIL.Emit(OpCodes.Ldstr, "are two bananas")
myIL.Emit(OpCodes.Br_S, endOfMethod)
myIL.MarkLabel(jumpTable(3))
myIL.Emit(OpCodes.Ldstr, "are three bananas")
myIL.Emit(OpCodes.Br_S, endOfMethod)
myIL.MarkLabel(jumpTable(4))
myIL.Emit(OpCodes.Ldstr, "are four bananas")
myIL.Emit(OpCodes.Br_S, endOfMethod)
myIL.MarkLabel(defaultCase)
myIL.Emit(OpCodes.Ldstr, "are many bananas")
myIL.MarkLabel(endOfMethod)
myIL.Emit(OpCodes.Ret)
myTypeBuilder.CreateType()
myAsmBuilder.Save("mia.dll")
Return (myTypeBuilder.CreateType())
End Function 'BuildMyType
Public Shared Sub Main()
Dim myType As Type = BuildMyType()
Dim theValue As Integer = Convert.ToInt32(InputBox("numero da 0 a 5"))
Dim myInstance As [Object] = Activator.CreateInstance(myType, New Object() {})
Dim ff As String = (myType.InvokeMember("SwitchMe", _
BindingFlags.InvokeMethod, Nothing, _
myInstance, New Object() {theValue}))
End Sub
End Class
but creating a dll which I posted above(Pblic Function makeit....)
any help will be appreciated!
Last edited by HanneSThEGreaT; March 5th, 2013 at 08:50 AM.
Reason: Added CODE Tags!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|