Well, I think, to some extent, it depends on what you plan to do after loading the assembly and what you have in the assembly.
I assume that you have some kind of plugin architecture, with a known interface or base class, which you are going to call methods, say, IPlugin.
In any case, here's how to load an assembly dynamically, based on storing a link to it in any configuration section or in a DB column somewhere:
private IPlugin LoadPlugin(string fullTypeName) { Type type = Type.GetType(fullTypeName, false, true); Object plugin = Activator.CreateInstance(type); if (plugin is IPlugin) { return (IPlugin) plugin; }
So, this will take a line such as "projA.PluginClass, projA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" load the assembly and return you an instance of the class you are interested in.
Then you will use it as follows:
// Call to DB to get details of class and assembly string pluginClass = GetPluginDetails(); IPlugin plugin = LoadPlugin(pluginClass); // Call known method to do something on IPlugin plugin.SomeMethod();
Zhaph - Ben Duguid
source share