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 = ""
            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

How do I do the same thing above, but read in the class file from a known directory?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Navneet.Net Full Stack DeveloperCommented:

Check this artcle

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.
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/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.