locked
Reflection Get Type from Assembly... RRS feed

  • Question

  • Is there a way to get a "type" in the form of a System.Type value from an Assembly variable?

    EX:

    dim Asm as Assembly = Assembly.Load("C:\MyDLL\MyAssembly.Dll")
    dim Typ as System.Type = Asm.GetType("MyObjectType")

    Jaeden "Sifo Dyas" al'Raec Ruiner

    (I know the above code is wrong, but that's the "idea" that I want to achieve)
    "Never Trust a computer. Your brain is smarter than any micro-chip."
    Monday, September 28, 2009 8:38 PM

Answers

  • You'll probably want to use Assembly.GetType .  This would look like:

        Dim Asm as Assembly = Assembly.Load("C:\MyDLL\MyAssembly.Dll")
        Dim Typ as System.Type = Asm.GetType("MyObjectNamespace.MyObjectType")

    Your code was nearly perfect - you probably just forgot to include the namespace.  This is very likely the same as the project's name (unless you've changed it), so for your "MyAssembly" project, it would look like:

        Dim Asm as Assembly = Assembly.Load("C:\MyDLL\MyAssembly.Dll")
        Dim Typ as System.Type = Asm.GetType("MyAssembly.MyObjectType")


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by eryang Monday, October 5, 2009 6:45 AM
    Monday, September 28, 2009 11:14 PM

All replies

  • Have a look at the static Type.GetType method (or one of its overloads). The Remarks section of that topic gives a good explanation plus a link to alternative approaches if you need to go the Assembly.Load route.
    Monday, September 28, 2009 9:12 PM
  • You'll probably want to use Assembly.GetType .  This would look like:

        Dim Asm as Assembly = Assembly.Load("C:\MyDLL\MyAssembly.Dll")
        Dim Typ as System.Type = Asm.GetType("MyObjectNamespace.MyObjectType")

    Your code was nearly perfect - you probably just forgot to include the namespace.  This is very likely the same as the project's name (unless you've changed it), so for your "MyAssembly" project, it would look like:

        Dim Asm as Assembly = Assembly.Load("C:\MyDLL\MyAssembly.Dll")
        Dim Typ as System.Type = Asm.GetType("MyAssembly.MyObjectType")


    Reed Copsey, Jr. - http://reedcopsey.com
    • Marked as answer by eryang Monday, October 5, 2009 6:45 AM
    Monday, September 28, 2009 11:14 PM