Implementing Plugins with Late Binding
Essentially everything you need to implement plugins in your application is some way to dynamically instantiate classes at runtime. As long as they all implement the same interface, you just use it to access its properties and methods.
In COM world this was achieved by calling the CreateObject
function. In the managed world you should use AppDomain.CreateInstanceAndUnwrap
as demonstrated by the following example:
IBind pluginInstance = (IBind)AppDomain.CurrentDomain.CreateInstanceAndUnwrap("MCPP",
"DamirsCorner.Samples.LateBinding.MCPP");
Everything you need to know at runtime is the full class name (including the namespace) and the containing assembly. To get those you can either require their previous registration within the application or you can dynamically discover them by loading assemblies from a predefined location (Assembly.Load
or Assembly.LoadFrom
) and enumerating their classes (Assembly.GetTypes
) whichever suits you situation best.