Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Late binding a dll from alternate folder

Status
Not open for further replies.

KPharm

Programmer
Feb 1, 2001
38
US
Hey guys -

We have an application that loads a specific DLL depending upon which "study" the user wants to access. This works just fine if all study-specific DLLs are located in the same folder as the main app's EXE.

How can we implement late binding to happen on a DLL in a sub-folder of the main folder?

root\MainApp.exe
root\Study1\Study.dll
root\Study1\OtherDLLsETC.dll

Putting all of the study-specific DLLs into the root folder will get nasty and may mean that we need to put the version numbers into the dll filenames, which is not good for us.

The late binding code we use is as follows:

Dim assem As System.Reflection.Assembly
Dim ty As Type

assem = System.Reflection.Assembly.Load(sStudyClassPrefix)

' sStudyClass must exist in the same folder as the EXE
ty = assem.GetType(sStudyClass)

oStudy = Activator.CreateInstance(ty)
 
After awhile and some hair-pulling, we figured it out. Instead of late-binding the class name (example, "MyClass") with reflection, late-bind the assembly from the full pathname of the DLL with reflection. Below, this example shows how a class is "reflected" to its inherited class.

MyClass inherits MyBaseClass. MyBaseClass contains the code that many versions of MyClass will use and MyClass can have specific logic that only it needs in it. This is imperative if your system needs to call a specific DLL based upon what user logs into the system, etc.

Enjoy!

Dim assem As System.Reflection.Assembly
Dim assemClass As AssemblyName
Dim ty As Type
Dim oMyClass as MyBaseClass

' We get the full pathname from the database to keep it "soft"
assemClass = AssemblyName.GetAssemblyName("C:\MyProject\MyClassDLL.dll")
assem = System.Reflection.Assembly.Load(assemClass)
ty = assem.GetType(MyDLL.MyClassName)
' Using CType here defeats the late-binding/Option-Strict problem.
oMyClass = CType(Activator.CreateInstance(ty), MyBaseClass.MyClassName)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top