首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

反射學習系列 3-反射實例應用 (三) 組態驅動與外掛架構圖解

     編輯:關於C語言

App.config 組態範例




  
    
    
    
  

同組件與跨組件載入

同組件:


Type type = Type.GetType(ConfigurationManager.AppSettings["LogType"]);
ILog log = (ILog)Activator.CreateInstance(type);
log.Write(new Exception("異常測試"));

跨組件:


string asmPath = Path.Combine(Environment.CurrentDirectory, "LogClassLibrary.dll");
Assembly asm = Assembly.LoadFrom(asmPath);
Type type = asm.GetType(ConfigurationManager.AppSettings["LogType"]);
ILog log = (ILog)type.InvokeMember(null, BindingFlags.CreateInstance, null, null, null);
log.Write(new Exception("異常測試"));

外掛架構結構圖

外掛架構結構圖

核心介面定義


public interface IHost
{
    List Plugins { get; }
    int LoadPlugins(string path);
    ILog GetLog(string name);
}
public interface ILog
{
    bool Write(string message);
    bool Write(Exception ex);
}

外掛程式設計要點:以介面定義外掛契約,宿主透過介面發現、載入、執行外掛。所有外掛實作必須實作該介面。反射的動態載入能力使此架構成為可能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved