一个简单的插件框架示例
閑言不講,直接上代碼,如下三個文件,分屬三個項目。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using Contract;
namespace Application
{
??? class Program
??? {
??????? /// <summary>??
??????? /// 構(gòu)建一個AppDomainSetup實例??
??????? /// 用于啟用卷影復制并設(shè)置基本路徑??
??????? /// </summary>??
??????? public static AppDomainSetup CreateAppDomainSetup()
??????? {
??????????? AppDomainSetup setup = new AppDomainSetup();
??????????? setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
??????????? setup.ShadowCopyFiles = "true";
??????????? return setup;
??????? }
??????? /// <summary>??
??????? /// 從當前目錄下的指定的程序集文件中加載指定的類型??
??????? /// </summary>??
??????? public static object CreateAndUnwrap(AppDomain appDomain, string assemblyFile, string typeName)
??????? {
??????????? string fullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyFile);
??????????? return appDomain.CreateInstanceFromAndUnwrap(fullName, typeName);
??????? }
??????? static void Main()
??????? {
??????????? Console.WriteLine("Current AppDomain:{0}",
??????????????? AppDomain.CurrentDomain.FriendlyName);
??????????? AppDomainSetup setup = CreateAppDomainSetup();
??????????? //建立準備加載插件的AppDomain??
??????????? AppDomain secAppDomain = AppDomain.CreateDomain("SecAppDomain", null, setup);
??????????? //忽略新建立的AppDomain里面的調(diào)用租約管理??
??????????? secAppDomain.DoCallBack(delegate
??????????? {
??????????????? LifetimeServices.LeaseTime = TimeSpan.Zero;
??????????? });
??????????? IAddin addinOne = (IAddin)CreateAndUnwrap(
?????????????????????????????????????????? secAppDomain, "Implement.dll", "Implement.AddinOne");
??????????? Console.WriteLine(addinOne.Run("Test"));
??????????? //卸載裝入插件的AppDomain??
??????????? AppDomain.Unload(secAppDomain);
??????????? //由于插件所在的AppDmain已被卸載,所以以下的執(zhí)行會出現(xiàn)異常??
??????????? try
??????????? {
??????????????? Console.WriteLine(addinOne.Run("Test"));
??????????? }
??????????? catch (Exception ex)
??????????? {
??????????????? Console.WriteLine("發(fā)生調(diào)用異常:" + ex.Message);
??????????? }
??????????? Console.ReadLine();
??????? }
??? }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Contract
{
??????? public interface IAddin
??????? {
??????????? string Run(string paramString);
??????? }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contract;
namespace Implement
{
??
?
?
??? public class AddinOne:MarshalByRefObject,IAddin??
??? {??
??????? public string Run(string paramString)??
??????? {??
??????????? const string resultString =??
??????????????? "Current AppDomain:{0},Param String:{1}!";??
?
??????????? return string.Format(??
??????????????? resultString,??
??????????????? AppDomain.CurrentDomain.FriendlyName,??
??????????????? paramString);??
??????? }??
??? }??
?
}
???
總結(jié)
以上是生活随笔為你收集整理的一个简单的插件框架示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《深入体验 飞鸽传书 开发内幕 核心基础
- 下一篇: 就要有鹤立鸡群的HTML5资本