Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load an assembly at run time that references the calling assembly

My application loads all library assemblies located in its executing path and executes preknown methods against contained classes.

I now need to do the same with an assembly that references my application assembly. Is this possible and are there any negative implications that I should be aware of?

Master Assembly:

public abstract class TaskBase 
{ 
    public abstract void DoWork(); 
}  

LoadAssemblyFromFile("Assembly0001.dll");  
Assembly0001.Task1.DoWork();  

Child Assemblies:

public sealed class Task1: MasterAssembly.TaskBase  
{ 
    public override void DoWork { /* whatever */ } 
}
like image 840
Raheel Khan Avatar asked Apr 15 '12 20:04

Raheel Khan


2 Answers

Yes, this is possible. As long as your master assembly doesn't reference the child assemblies, you should be fine. Otherwise, you'll have a circular dependency.

The master assembly will simply load the child assemblies and know nothing about them except that they implement an interface. That way, the master assembly doesn't need to reference the child assemblies.

No gotchas as far as I'm aware. We use this technique successfully for certain scenarios.

like image 95
Bob Horn Avatar answered Oct 30 '22 15:10

Bob Horn


In my experience there is nothing wrong with this. In fact, MEF uses this technique in the form of AssemblyCatalog (where your implementations are IN the master assembly) and DirectoryCatalog (where the implementations of an interface are in assemblies in a specific directory).

Both can be used together in an AggregateCatalog with no problems.

like image 33
Steve Danner Avatar answered Oct 30 '22 14:10

Steve Danner