Plugin is a very flexible extensible, modular mechanism, Microsoft's application block many of the use of plugin structure, this article discusses plugin in C # a practice.
Plugin is a unified interface, which uses reflection mechanism in C # to produce the object of realizing IPlugin. Assuming the interface is IPlugin, the following block of code signals the code snippet that dynamically produces the IPlugin object, where dllname is the DLL file name that implements the IPlugin interface class:
Public IPlugin Loadplugin (string dllName)
{
Assembly asm = AppDomain.CurrentDomain.Load (dllName);
foreach (Type t in ASM. GetTypes ()) {
foreach (Type iface in T.getinterfaces ()) {
if (iface. Equals (typeof (IPlugin))) {
Return (System.Activator.CreateInstance (t)) as IPlugin;
}
}
}
return null;
}
Java also has a reflection mechanism, the implementation of plugin is also very simple, roughly (IPlugin) class.forname (className). The look of newinstance (). C + + Because of the reflection mechanism, DLL emulation can be used to export a global function and interface in a DLL that returns a pointer to an object that implements the interface.
Microsoft has an article on provider pattern, which provides another example of how C # can be instantiated with parameters.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.