chiname

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

如何用实现.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为程序集的具体版本、文化背景、签名,没有特定要求,这些都可以省略。

我们可以根据类型的名称,来动态载入一个所需要的类型。如:

string typeName = "System.Configuration.NameValueSectionHandler, System";
Type t 
= Type.GetType(typeName);
Object obj 
= Activator.CreateInstance(t);
//
System.Configuration.NameValueSectionHandler obj = (System.Configuration.NameValueSectionHandler)Activator.CreateInstance(t);

此时,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// 不是所需要的插件
}

总结上述代码,我们可以做出通用的加载插件的代码:

/// <summary>
/// 动态装载并创建类型,该类型拥有指定接口
/// </summary>
/// <param name="className">类型名称</param>
/// <param name="interfaceName">指定的接口名称</param>
/// <param name="param">指定构造函数的参数(null或空的数组表示调用默认构造函数)</param>
/// <returns>返回所创建的类型(null表示该类型无法创建或找不到)</returns>

public static object LoadObject(string className, string interfaceName, object[] param)
{
 
try
 
{
  Type t 
= Type.GetType(className);
              
  
if ( t == null 
   
|| !t.IsClass
   
||  !t.IsPublic 
   
||  t.IsAbstract
   
||  t.GetInterface(interfaceName) == null)
  
{
   
return null;
  }


  
object o = Activator.CreateInstance(t, param);
  
if( o == null )
  
{
   
return null;
  }

    
  
return o;
 }

 
catch( Exception ex )
 
{
  
return null;
 }

}


以后,我们就可以使用LoadObject载入任何所需的插件。

插件一般放在配置文件中,并由程序读入:
配置文件举例(配置文件的使用参见我的相关随笔):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<configSections>
        
<section name="Channels" type="Vmp.Configuration.ChannelsSectionHandler, Communication" />
    </configSections>
    
    
<Channels>
        
<channel
            
ChannelType="Vmp.Communication.TcpChannel, Communication" 
            TraceFile
="d:\log\channel1.log"
            Port
="2020" MaxConnections="300" BufferSize="2048"
        
/>
    
</Channels>
</configuration>

代码范例:

private ArrayList channelsList = new ArrayList();

private LoadChannels()
{
    ArrayList channelsConfig 
= (ArrayList)ConfigurationSettings.GetConfig( "Channels" );
    
foreach(Hashtable config in channelsConfig)
    
{
        
string channelType    = (string) config["ChannelType"];

        IChannel channel 
= (IChannel) CommonUtils.LoadObject(channelType, typeof(IChannel).FullName, new object[]{config});
        
if(channel == null)
            
continue;

        channelsList.Add(channel);
}



也可以遍历指定的插件目录,并载入所有符合要求的插件,例如:

public IPlugin[] LoadAllPlugIn(string pluginDir)
{
    
// 设置默认的插件目录
    if(pluginDir == null || pluginDir == "")
        pluginDir 
= "./PlugIns";

    
// 获取插件接口名称
    string interfaceName = typeof(IPlugin).FullName;

    
// 用于存放插件的数组
    ArrayList arr = new ArrayList();

    
// 遍历插件目录(假设插件为dll文件)
    foreach(string file in Directory.GetFiles(pluginDir, "*.dll"))
    
{
        
// 载入插件文件
        Assembly asm = Assembly.LoadFile(file);
        
// 遍历导出的插件类
        foreach(Type t in asm.GetExportedTypes())
        
{
            
// 载入插件,如果插件不符合指定的接口,则返回null
            IPlugin plugin = LoadObject(t.FullName, interfaceName, nullas IPlugin;

            
if(plugin != null)
                arr.Add(plugin);
        }

    }


    
// 返回插件
    return (IPlugin[])arr.ToArray(typeof(IPlugin));
}
posted on 2005-09-06 16:04 沐枫 阅读(361) 评论(7)  编辑 收藏 收藏至365Key 所属分类: .NET FX

Feedback

# re: 如何用实现.NET的插件机制 2005-09-06 16:57 可冰
那么用C++\C如何来实现这样的机制呢?或是根本就不可以?
  

# re: 如何用实现.NET的插件机制 2005-09-06 17:13 Kevin
http://jiezhi.cnblogs.com/archive/2005/07/07/186757.html
  

# re: 如何用实现.NET的插件机制 2005-09-06 18:25 沐枫
C/C++的插件机制和.NET根本不一样。
.NET 大多数插件实现都是使用 Reflection 机制。
而C/C++没有标准的方法,不过Windows下常见的有两种:DLL入口,COM接口。
  

# re: 如何用实现.NET的插件机制 2005-09-06 18:30 双鱼座
看着你这个LoadObject,不爽。
1.param参数多余。谁来提供这个参数?配置文件无法提供这个参数。
2.不必提供接口类型参数,因为插件接口是约定的。即使提供也不要采用接口名方式,应该直接提供类型,Type类型,例如typeof(IPlugin),
3.判断方式不爽,也未报告不能载入插件的原因。

public IPlugin LoadPlugin(string typeName) {
Type t = Type.GetType(typeName);
if (t == null) {
...报告类未发现
} else if (t.IsAbstract) {
...报告不能载入抽象类
} else if (! typeof(IPlugin).IsAssignableFrom(t)) {
...报告插件未实现IPlugin接口
} else {
return Activator.CreateInstance(t) as IPlugin;
}
}
  

# re: 如何用实现.NET的插件机制 2005-09-06 20:45 沐枫
你说的很对。
1 param确是多余。特地在此列出实在是蛇尾。
2 接口类型参数在此列出,是为了让LoadObject有通用的功能。
使用接口名其实挺不错的,因此在配置文件提供的内容基本上是字符串。
3 判断方式不爽,我也觉得。
实际上是是临时从我的实现中删去了错误报告,并直接改为return null。
我的应用中,此处为记录错误日志,并throw一个例外。
  

# re: 如何用实现.NET的插件机制 2005-09-07 02:54 yyanghhong
spring.net不就是干这个吗?
  

# re: 如何用实现.NET的插件机制 2005-09-07 08:43 gozh2002
two things need to be commented furthur

1. How about loading the class type (I think it should be actually an assembly dll) to a second application domain, which gives you the ability to load/unload when application running (hot swap)...

2. Need a demo of scenairo using constructor injection or setter injection to set object of your application side to for the plug-in object access. (really spring.net :)
posted on 2005-09-07 09:09  把我的欢乐带给你  阅读(162)  评论(0)    收藏  举报