如何用实现.NET的插件机制
如何用实现.NET的插件机制
.NET 提供的反射(Reflection)机制可以很方便的加载插件。本文提供一种方法,可以灵活的正确的载入所需的插件。
在.NET中,一个完整的类型名称的格式如 "类型名, 程序集名"。
例如:"System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"。
- 类型名为:System.Configuration.NameValueSectionHandler,这是带名字空间的完整类型名。
你也可以使用该类型的FullName得到。
如:string typeName = typeof(NameValueSectionHandler).FullName;- 程序集名为:"System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
程序集名为System,系统为自动为其适配扩展名(如System.dll或System.exe);
Version、Culture、PublicKeyToken为程序集的具体版本、文化背景、签名,没有特定要求,这些都可以省略。
我们可以根据类型的名称,来动态载入一个所需要的类型。如:
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
此时,obj 就是所需要的类型实例。
通常的插件,是需要实现一定的接口的类。因此,在载入插件之前,需要确定该插件类型是否是合适的。
比如,一个插件的接口为 IPlugin,那么我们可以用如下方式来识别:
string interfaceName = typeof(IPlugin).FullName;
string typeName = "Muf.MyPlugin, MyPlugin";
Type t = Type.GetType(typeName);
if ( t == null
|| !t.IsClass
|| !t.IsPublic
|| t.GetInterface(interfaceName) == null)
{
return null; // 不是所需要的插件
}
string typeName = "Muf.MyPlugin, MyPlugin";
Type t = Type.GetType(typeName);
if ( t == null
|| !t.IsClass
|| !t.IsPublic
|| t.GetInterface(interfaceName) == null)
{
return null; // 不是所需要的插件
}
总结上述代码,我们可以做出通用的加载插件的代码:
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
以后,我们就可以使用LoadObject载入任何所需的插件。
插件一般放在配置文件中,并由程序读入:
配置文件举例(配置文件的使用参见我的相关随笔):
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
代码范例:
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
也可以遍历指定的插件目录,并载入所有符合要求的插件,例如:
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://laiwen.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)