灵活正确的实现.NET插件机制开发者在线 Builder.com.cn 更新时间:2008-08-05作者: 来源:开发者在线...

灵活正确的实现.NET插件机制 开发者在线 Builder.com.cn更新时间: 2008-08-05作者: 来源:开发者在线本文关键词: 机制 插件 .net    在.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:logchannel1.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[]);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, null) as IPlugin;
  if(plugin != null)
  arr.Add(plugin);}
  // 返回插件return (IPlugin[])arr.ToArray(typeof(IPlugin));}



转载于:https://www.cnblogs.com/zhiji6/archive/2008/12/29/1649388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值