Guidance on using drop-in DLLs
I have been given the task of rewriting an internal utility in .Net for my work. One of the program requirements of the new system is to have a DLL that implements a set of interfaces and have the program call the DLL.
Now this DLL will be changed out a lot per deployment. My question is what is the best way to do it from a development standpoint? Do I add a template DLL (on开发者_JAVA技巧e that only has the interfaces but no implementation) to the project references like I would do any other DLL that I would use?
Or do I need to use something like this every time I want to use code from the DLL?
var DropIn = System.Reflection.Assembly.LoadFrom("DropInDll.dll");
var getActions = DropIn.GetType("Main").GetMethod("GetActions");
List<IAction> ActionList = (List<IAction>)getActions.Invoke(null, null);
Use Dependency Injection with an Inversion of Control Container.
Since you are already coding to defined interfaces, you shouldn't need any reflection.
Here's an example using the Common Service Locator from CodePlex, specifically the Simple Service Locator implementation.
Say you have a IDropIn interface, and different DLLs that implement the interface.
First, you need to register your interface with the system:
void Init()
{
// Read dropInDllName and dropInClassName from your config file
Assembly assembly = Assembly.Load(dropInDllName);
IDropIn dropIn = (IDropIn)assembly.CreateInstance(dropInClassName);
SimpleServiceLocator container = new SimpleServiceLocator();
container.RegisterSingle<IDropIn>(dropIn);
Microsoft.Practices.ServiceLocation.ServiceLocator.SetLocatorProvider(() => container);
}
Then, to get an instance in your code somewhere else, do this:
IDropIn dropIn = ServiceLocator.Current.GetInstance<IDropIn>();
List<IAction> actionList = dropIn.GetActions();
精彩评论