一个简单的插件框架示例
2012-06-26 17:22
477 查看
闲言不讲,直接上代码,如下三个文件,分属三个项目。
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>
/// 构建一个AppDomainSetup实例
/// 用于启用卷影复制并设置基本路径
/// </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里面的调用租约管理
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已被卸载,所以以下的执行会出现异常
try
{
Console.WriteLine(addinOne.Run("Test"));
}
catch (Exception ex)
{
Console.WriteLine("发生调用异常:" + 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);
}
}
}
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>
/// 构建一个AppDomainSetup实例
/// 用于启用卷影复制并设置基本路径
/// </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里面的调用租约管理
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已被卸载,所以以下的执行会出现异常
try
{
Console.WriteLine(addinOne.Run("Test"));
}
catch (Exception ex)
{
Console.WriteLine("发生调用异常:" + 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);
}
}
}
相关文章推荐
- node学习(4)一个简单的express框架的示例
- 深入浅出学习Struts1框架:一个简单mvc模式代码示例开始
- 使用TestBean框架开发一个简单的sampler插件
- Chrome 插件 一 一个简单的插件示例
- Xqk.Data数据框架使用说明之:一个简单的示例
- js实现一个简单的MVVM框架示例
- 使用Python的Bottle框架写一个简单的服务接口的示例
- 一个简单的Socket游戏服务器框架示例.
- 深入浅出学习Struts框架(一):一个简单mvc模式代码示例开始
- 深入浅出学习Struts1框架(一):一个简单mvc模式代码示例开始
- 深入浅出学习Struts1框架(一):一个简单mvc模式代码示例开始
- Android 框架设计Demo,一个简单的MVP示例搜索功能,网络请求用Retrofit+RxJava实现
- 一个超级简单的HTML模板框架源代码以及使用示例
- 基于boost的bind与function的一个简单示例消息处理框架
- 使用Python的Bottle框架写一个简单的服务接口的示例
- 深入浅出学习Struts1框架(一):一个简单mvc模式代码示例开始
- 三分钟实现一个插件平台和一个插件——基于最简单的OSGi.NET插件框架
- 一个jQuery插件框架示例 by FungLeo
- 一个自己开发的简单,快速,方便,小巧的php web框架
- Dubbo入门---搭建一个最简单的Demo框架