基于微软MEF,实现代码间依赖协议而不是具体实现或指定接口
public class MyClass
{
[Export("加密")]
public static string Encryption(string str)
{
if (string.IsNullOrEmpty(str))
{
return string.Empty;
}
return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
}
}
static class Components
{
static Components()
{
MEF.Import(typeof(Components));
}
[Import("加密")]
public static Func<string, string> Encryption;
}
static void Main(string[] args)
{
Console.WriteLine(Components.Encryption("")); //空字符串
Console.WriteLine(Components.Encryption("aaaa")); //YWFhYQ==
}
public class MyClass2
{
[Export("加密")]
[ExportMetadata("Priority", 1)] //增加优先级
public static string Encryption(string str)
{
if (string.IsNullOrEmpty(str))
{
return Guid.Empty.ToString("n");
}
return Convert3.ToMD5(str);
}
}
static void Main(string[] args)
{
Console.WriteLine(Components.Encryption("")); //00000000000000000000000000000000
Console.WriteLine(Components.Encryption("aaaa")); //74b87337454200d4d33f80c4663dc5e5
}
无需定义,直接获取并使用
static void Main(string[] args)
{
Func<string,string> encode = MEF.PlugIns.GetExport<Func<string, string>>("加密");
Console.WriteLine(encode("aaaa")); //74b87337454200d4d33f80c4663dc5e5
}
- 新增一个功能类
CallContextEx
用于系统对象CallContext
的功能拓展 - 修复在逻辑上下文中存在无法序列化的对象,则大部分插件加载失败的问题
- 优化2017单元测试中的MEF加载路径和方式
- 修复输入完整日志时报错的bug
- 紧急修复bug
- 提供
MEF.ReInitiation
方法,手动重载插件 - 当插件中出现无效插件时,自动重新载入所有插件
- 开放
ServiceContainer
类中多个方法的重写
- 优化日志拓展方法,当
message
参数为空时,不再抛出异常
- 优化插件对优先级的处理
- 修复bug
- 新增一组方法用于获取匹配的插件
PlugInContainer.GetPlugIns
- 新增
SimpleTraceListener
侦听器基类 - 新增
ServiceContainer
的日志记录
- 优化
ServiceContainer
可以根据优先级过滤插件
- 修复部分bug
- 优化日志
- 优化默认插件的行为
ComponentServices.ToJsonObject
和ComponentServices.ToJsonString
2个方法在没有插件时,默认会寻找Newtonsoft.Json
- 优化插件加载,当1个插件加载失败时,不会影响其他插件
- 修复默认插件为空的bug
- 修复一个很诡异的bug
- 重构部分功能
- 优化加载逻辑,防止相同的组件多次加载
- 优化多线程并发初始化时加载逻辑
- 优化Type加载失败时的处理逻辑
- 优化代码