首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 虚拟主机评测对比
灵活正确的实现.NET插件机制-.NET教程,.NET Framework
作者:网友供稿 点击:123
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
.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));
}

转自:动态网站制作指南 | www.knowsky.com

文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·Asp.Net下导出/导入规则的Excel(.xls)文件-ASP教程,ASP应用
·中国万网,万网
·中资源
·上海火速
·加拿大中文网
·华夏名网
·世纪东方
·商务中国
·中国E动网
·神话网络
最新文章
·photoshop抠图合成特效:飞出照片相框_photoshop教程
·将升级的windows vista驱动还原到原来的版本_windows vista
·photoshop漫画笔刷绘制非主流照片漫画_photoshop教程
·看域名注册信息判断网赚公司真假_网赚技巧
·找坐标:对网站进行坐标定位_站长心得
·如何增加网站pv_站长心得
·google性格一:对活跃度高的网页收录很快_google推广
·googleadsense容易被k的可能性列表_google推广
·价值数万的googlepr作弊方_google推广
·如何抢占人家的“首要意念”_google推广
相关主题
西部数码虚拟主机

友情链接
CNNIC 西部数码
万网 自助建站
虚拟主机 asp空间
域名注册 域名
域名申请 主页空间
论坛空间 网站空间
国际域名 虚拟空间
空间租用 DDOS防火墙
成都主机托管 四川主机托管
主机租用 服务器租用
网站目录 自助建站
虚拟主机 网址大全
软件下载
自助链接
虚拟主机资讯 特价虚拟主机
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
发表评论 打印  刷新     关闭