zoukankan      html  css  js  c++  java
  • 實現.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, null) as iplugin;

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

        // 返回插件
        return (iplugin[])arr.toarray(typeof(iplugin));
    }

  • 相关阅读:
    Quartz使用总结
    ubuntu 16.04 下载源
    samba搭建
    搭建FTP服务器
    ubuntu 快捷图标
    mysql Fatal error encountered during command execution
    vs2013调试的时候卡顿
    javascript父窗口与子窗口通信
    mysql设置字体
    前台声明变量
  • 原文地址:https://www.cnblogs.com/twttafku/p/1043388.html
Copyright © 2011-2022 走看看