Dynamically loading class files in vb.net

I have an application that I am creating that will dynamically read in the assembly and class name of a particular object I want and create an instance of it, so that I may assign and use it's unique properties.  This works great, however, I now want to house the .vb (class files) in a resource folder and read them in and create instance of them from there so that I can use their properties, instead of adding the classes into my project for each new item I need.  This will allow me to easily add new items on the fly.  The current code I am using reads in the values from an XML file:

        Dim theList = LoadExportXML(aPersist.GetType().ToString())
        Dim thefootername As String = ""
        Dim thefooternamespace As String = ""
        Try
            For Each theParent As XmlNode In theList
                If theParent.Attributes("hasfooter").Value = "Y" Then
                    ' get delimiter
                    'dg.rts.domain.CycleCountHeader, dg.rts
                    thefootername = theParent.Attributes("footername").Value
                    thefooternamespace = theParent.Attributes("footernamespace").Value
                    Dim thefooter As BaseFooter
                    Dim thetype = Type.GetType(thefootername & "," & thefooternamespace)
                    thefooter = Activator.CreateInstance(thetype)
                    thefooter.DocumentNumber = aPersist.DocumentNumber
                    thefooter.TotalPrice = TotalPrice
                    thefooter.TotalQty = TotalQty
                    thefooter.TotalRecords = TotalRecords
                    thefooter.NumberOfCartons = NumberOfCartons
                    ExportRecord(thefooter, 0)

                End If
            Next

How do I do the same thing above, but read in the class file from a known directory?
SNichols1124Asked:
Who is Participating?
 
multithreadingCommented:
You are missing one piece. You need another assembly to act as the intermediary between the assembly that is dynamically loading the target assemblies, and the dynamically loaded assemblies. Each of the classes you are dynamically loading needs to support one (or more) interfaces from that linkage assembly. Then when you instantiate classes from the target assembly, the app code calls methods on the dynamically loaded classes only via the interfaces.

With this done, both the code loading the assemblies on the fly, and the assemblies being loaded, both have references to the interface-only assembly. The interface-only assembly has references to neither project. Your app level code (that is loading the assemblies) has a reference only to the interface-assembly. Now you are good to go. You can have a folder with as many assemblies as you want, without references to any of them.

By the way, because this is such a common requirement there is a support library that makes working with dynamically loaded assemblies very nice. It is called the "Managed Extensibility Framework" - "MEF". You probably want to get this basic dynamic-load scenario under your belt before diving into MEF, just so you get the concepts down before complicating your life even more, but after you've mastered the basics, if you want to take this farther MEF is a good way to go.  The MEF site is on CodePlex: http://mef.codeplex.com/
0
 
NavneetSoftware EngineerCommented:
H!

Check this artcle
http://forums.whirlpool.net.au/archive/628754

Thanks!
0
 
TommySzalapskiCommented:
Classes are compiled unlike XML the compiler needs the class definition in order to use the class. You cannot load new classes at runtime. The compiler needs to know what to expect. You will have to add the references and using directives like you have been doing.
0
 
SNichols1124Author Commented:
Created an Interface and mulitple assemblies for each type I needed.  Implemented the interface in each of the assemblies.  Works like a charm.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.