惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
小众软件
小众软件
月光博客
月光博客
D
DataBreaches.Net
L
LangChain Blog
美团技术团队
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Y
Y Combinator Blog
I
InfoQ
U
Unit 42
Microsoft Azure Blog
Microsoft Azure Blog
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
腾讯CDC
Martin Fowler
Martin Fowler

博客园 - Tristan(GuoZhijian)

Core Design Patterns(16) Chain of Responsibility 职责链模式 Core Design Patterns(15) Template Method 模版方法模式 Core Design Patterns(14) State 状态模式 Core Design Patterns(13) Strategy 策略模式 Core Design Patterns(12) Builder 建造者模式 Core Design Patterns(11) Abstract Factory 抽象工厂模式 Core Design Patterns(10) Singleton 单例模式 Core Design Patterns(9) Factory Method 工厂方法模式 Core Design Patterns(8) Prototype 原型模式 Core Design Patterns(7) Facade 外观模式 Core Design Patterns(6) Adapter 适配器模式 Core Design Patterns(5) Flyweight 享元模式 Core Design Patterns(4) Composite 组合模式 Core Design Patterns(3) Bridge 桥接模式 Core Design Patterns(2) Proxy 代理模式 Core Design Patterns(1) Decorator 装饰模式 Behavior模型应用:可拖动的div容器 Microsoft Asp.Net Ajax框架入门(13) PageRequestManager Microsoft Asp.Net Ajax框架入门(12) 了解异步通信层
老调重弹:插件式框架开发的一个简单应用
Tristan(GuoZhijian) · 2008-02-23 · via 博客园 - Tristan(GuoZhijian)

VS 2008

    最近要做一个应用程序检测程序,就是要检测服务器上各应用程序的运行情况,由于各应用程序的差异,很难做一个统一的探测程序,于是决定对任意一个应用程序都采用独立的一条探测规则。
    为了开发、部署的方便,考虑使用插件式开发。

本文案例包含三个项目:
1) Tristan.DetectContract 
    插件接口(契约),定义了探测的行为,以及传递的参数
2) Tristan.DetectCenter
    探测主程序,引用Tristan.DetectContract
3) CompareSvcDetector
    插件1,用于探测一个名为"CompareSvc"的应用程序的插件,引用 Tristan.DetectContract

1. 插件接口
    
1.1 IDetector

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tristan.DetectContract {
    
public interface IDetector {

        
/// <summary>
        
/// 线程控制开关
        
/// </summary>

        bool ThreadFlag {
            
get;
            
set;
        }

        
/// <summary>
        
/// 探测
        
/// </summary>
        
/// <param name="onNormal"></param>
        
/// <param name="onError"></param>

        void Detector(Action<DetectEventArgs> onNormal, Action<DetectEventArgs> onError);
    }

}

1.2 DetectEventArgs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tristan.DetectContract {
    
public class DetectEventArgs {
        
/// <summary>
        
/// 探测结果
        
/// </summary>

        public DetectResult DetectResult {
            
get;
            
set;
        }

        
/// <summary>
        
/// 探测任务编码
        
/// </summary>

        public string DetectCode {
            
get;
            
set;
        }

        
/// <summary>
        
/// 提示信息
        
/// </summary>

        public string Message {
            
get;
            
set;
        }

    }

    
public enum DetectResult 
        Normal,
        Error
    }

}


    

2. 探测程序

using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using Tristan.DetectContract;

namespace Tristan.DetectCenter {
    
class Program {
        
static void Main(string[] args) {
            
//从指定文件夹读取dll
            string[] files = Directory.GetFiles("PlugIn""*.dll");
            
foreach (var f in files) {
                
//通过反射加载程序集
                Assembly assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + f);

                
//获取程序集内定义的类
                Type[] types = assembly.GetTypes();
                
foreach (Type t in types) {

                    
//判断类 t 是否实现了 IDetector接口
                    IDetector d = Activator.CreateInstance(t) as IDetector;
                    
if (d != null{
                        
//置探测线程开关
                        d.ThreadFlag = true;
                        
//开始探测
                        d.Detector(new Action<DetectEventArgs>(SucceedHandler), new Action<DetectEventArgs>(ErrorHandler));
                    }

                }

            }

            Console.Read();
        }

        
static void SucceedHandler(DetectEventArgs e) {
            Console.WriteLine(e.DetectCode 
+ "," + e.DetectResult.ToString() + "," + e.Message);
        }

        
static void ErrorHandler(DetectEventArgs e) {
            Console.WriteLine(e.DetectCode 
+ "," + e.DetectResult.ToString() + "," + e.Message);
        }

    }

}

3. 插件1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Tristan.DetectContract;

namespace CompareSvcDetector {
    
public class CompareSvcDetector : IDetector {

        
private string detectCode = "XX0001";

        
IDetector Members
    }

}

    这里,我通过生成随机数的方式来模拟探测到应用程序的正常和异常状态。

    编译CompareSvcDetector得 CompareSvcDetector.dll,将该文件置于 Tristan.DetectCenter安装目录的 PlugIn 文件夹下

运行: