CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2013
    Posts
    2

    Question 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!

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Reflection.emit dll

    Hi Welcome to the forum!

    Please have a read here :

    http://forums.codeguru.com/showthrea...for-more-info)

    So that we all could read your code, and that it is more easier to determine your potential problem.

  3. #3
    Join Date
    Mar 2013
    Posts
    2

    Re: Reflection.emit dll

    wow...after 24 hrs only one reply(not solving my problem...)

    Very nice site...you should change the name in "Ghostguru.com" lol

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Reflection.emit dll

    Why you got no decent replies could be due to the fact that your code is not formatted correctly, as I've stated in my previous post.

    ...................I will do it for you now.....................

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured