Results 1 to 6 of 6

Thread: Finding a class in an assembly?

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    15

    Finding a class in an assembly?

    My program compiles script files written in VB.NET at run-time into an assembly in memory using the built-in compiler stuff. Now, let's say my script has this in it:

    VB Code:
    1. Namespace Testing
    2.     Public Class Abc
    3.         Public Sub TryMe()
    4.             System.Windows.Forms.MessageBox.Show("Hello World")
    5.         End Sub
    6.     End Class
    7. End Namespace

    Well, my program will take this script and compile it at run-time into an assembly in memory.

    Is there a way to get a list of the namespaces/classes/etc. contained within the assembly, or search through it for a class with a certain name and return its information?

    I'm pretty new to assembly stuff and compiling dynamic code at run-time, so I'm not really sure how to do much yet. Any help would be greatly appreciated.

    Thanks in advance,
    Satch

  2. #2
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Maybe you can do something like this.....

    Code:
    Dim asm As System.Reflection.Assembly
    asm = System.Reflection.Assembly.LoadFrom("c:\asm.dll")
    Dim ty As Type
    For Each ty In asm.GetTypes
        MsgBox(ty.FullName)
    Next
    I'm not sure how to get a proper reference to the class in your case, if LoadFrom does not work, try Load or any of the other commands.

  3. #3
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    of course u can..do a search by the forum and u'll c that i made the same question a while ago..

    and by what u say i see that u used the system.reflection.emit ... i am very interested in that but where can i find info about how to use it? tks
    \m/\m/

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    15
    Thanks for the responses

    PT, I found the post and can't wait to get home to try it out. As for the System.Reflection.Emit stuff, I'm not sure if its the same as what I'm doing. I use the Microsoft.VisualBasic.VBCodeProvider and the compiler in System.CodeDOM (can't remember the full name at the moment.) Either way, it'll compile VB.NET code. I'll post a link to it when I get home where I have access to my project's code.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2000
    Location
    Texas
    Posts
    15
    This compiles the compile.vb file in the application's directory and outputs it into assembly.dll and loads it into the memory.

    VB Code:
    1. Dim myVBCodeProvider As Microsoft.VisualBasic.VBCodeProvider
    2. Dim myCodeCompiler As CodeDom.Compiler.ICodeCompiler
    3. Dim myCompilerParameters As CodeDom.Compiler.CompilerParameters
    4. Dim myCompilerResults As CodeDom.Compiler.CompilerResults
    5. Dim i As Long
    6.  
    7. myCompilerParameters = New CodeDom.Compiler.CompilerParameters()
    8. myCompilerParameters.GenerateInMemory = False
    9. myCompilerParameters.GenerateExecutable = False
    10. myCompilerParameters.OutputAssembly = Application.StartupPath + "\assembly.dll"
    11.  
    12. myVBCodeProvider = New Microsoft.VisualBasic.VBCodeProvider()
    13.  
    14. Dim asm As [Assembly]
    15.  
    16. For Each asm In AppDomain.CurrentDomain.GetAssemblies
    17.       myCompilerParameters.ReferencedAssemblies.Add(asm.Location)
    18. Next
    19.  
    20. myCodeCompiler = myVBCodeProvider.CreateCompiler()
    21. myCompilerResults = myCodeCompiler.CompileAssemblyFromFile(myCompilerParameters, Application.StartupPath + "\compile.vb")
    22.  
    23. If myCompilerResults.Errors.HasErrors Then
    24.       For i = 1 To myCompilerResults.Errors.Count
    25.             frmMain.DefInstance.lstStatus.Items.Add("Error: " + myCompilerResults.Errors.Item(i - 1).ToString())
    26.             Application.DoEvents()
    27.       Next
    28.       Exit Sub
    29. End If
    30.  
    31. Compiler_Assembly = myCompilerResults.CompiledAssembly

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    ahh tks...but im in C#...and looks like no one has examples about using system.reflection.emit
    \m/\m/

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