MEF(Managed Extensibility Framework)有选择性地使用扩展组件
在"MEF(Managed Extensibility Framework)使用全部扩展组件"中,客户端应用程序调用了所有的扩展组件,而且如果有新的扩展组件加入,必须先关闭程序,再重新启动才可以调用所有组件。
本篇体验使用MEF的ExportMetadata特性,有选择性地使用某些扩展组件,使用Lazy<>,让客户端程序延迟动态加载组件,即使不关闭应用程序,也能调用所有组件。
● StudentMEF.Lib, 类库,包含接口IStudent.cs
● StudentMEF.One,类库,包含实现IStudent的类One.cs
● StudentMEF.Two,类库,包含实现IStudent的类One.cs
● StudentMEF.Client,控制台程序
□ 所有扩展满足的约定是
namespace StudentMEF.Lib
{
public interface IStudent
{
string GetName();
int GetScore();
}
}
□ StudentMEF.One类库
→ 需引用StudentMEF.Lib组件
→ 需引用System.ComponentModel.Composition组件
using System.ComponentModel.Composition;
using StudentMEF.Lib;
namespace StudentMEF.One
{
[Export(typeof(IStudent))]
[ExportMetadata("Type","ONE")]
public class One : IStudent
{
private string _Name;
private int _Score;
public One()
{
_Name = "jack";
_Score = 80;
}
public string GetName()
{
return _Name;
}
public int GetScore()
{
return _Score;
}
}
}
□ StudentMEF.Two类库
→ 需引用StudentMEF.Lib组件
→ 需引用System.ComponentModel.Composition组件
using System.ComponentModel.Composition;
using StudentMEF.Lib;
namespace StudentMEF.Two
{
[Export(typeof(IStudent))]
[ExportMetadata("Type","TWO")]
public class Two : IStudent
{
private string _Name;
private int _Score;
public Two()
{
_Name = "Bob";
_Score = 90;
}
public string GetName()
{
return _Name;
}
public int GetScore()
{
return _Score;
}
}
}
□ StudentMEF.Client控制台程序
→ 需引用StudentMEF.Lib组件
→ 需引用System.ComponentModel.Composition组件
→ 需在可执行程序所在目录,即生成路径下,创建Extensions文件夹
需要一个帮助类StudentFactory,他的职责包括:
1、为了动态加载Extensions文件夹的所有扩展组件,可以使用Lazy<>来动态实现延迟加载,即只有当客户端程序使用到Extensions文件夹中组件的时候,再去加载这些扩展组件。
2、把扩展组件放到Catalog中,在CompositionContainer构建Part等,这些常规操作,也都需要。
3、提供一个方法,根据元数据ExportMetadata来获取对应的组件。
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using StudentMEF.Lib;
namespace StudentMEF.Client
{
public class StudentFactory
{
[ImportMany]
private Lazy<IStudent, IDictionary<string, object>>[] Students { get; set; }
public StudentFactory()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new DirectoryCatalog(@".\Extensions"));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
public List<IStudent> GetStudents(string c)
{
List<IStudent> result = new List<IStudent>();
foreach (var student in Students)
{
if ((string)student.Metadata["Type"] == c)
{
result.Add(student.Value);
}
}
return result;
}
}
}
Lazy<IStudent, IDictionary<string, object>>中,第二个参数IDictionary<string, object>与ExportMetadata("Type","TWO")对应。
static void Main(string[] args)
{
string c = "";
while (true)
{
Console.WriteLine("输入学生类型:");
c = Console.ReadLine();
StudentFactory studentFactory = new StudentFactory();
List<IStudent> students = studentFactory.GetStudents(c.ToUpper());
if (students.Count == 0)
{
Console.WriteLine("没有此类型学生~");
}
else
{
foreach (IStudent student in students)
{
Console.WriteLine(string.Format("姓名:{0},分数:{1}",student.GetName(),student.GetScore()));
}
}
}
Console.ReadKey();
}
当Extensions文件夹中只有StudentMEF.One.dll组件,输入one,找到匹配,输入two找不到匹配;不关闭控制台程序,把StudentMEF.Two.dll也放入 Extensions文件夹,再输入two,找到匹配。
MEF(Managed Extensibility Framework)有选择性地使用扩展组件的更多相关文章
- MEF(Managed Extensibility Framework)使用全部扩展组件
MEF(Managed Extensibility Framework),所在命名空间是System.ComponentModel.Composition.dll.简单来说,MEF是将符合约定(一般是 ...
- MEF(Managed Extensibility Framework) 微软平台插件化开发
体验Managed Extensibility Framework精妙的设计 MEF(Managed Extensibility Framework)是.NET Framework 4.0一个重要 ...
- MEF(Managed Extensibility Framework)依赖注入学习
MSDN官方资料,并且微软还提供了SimpleCalculator sample学习样例 http://msdn.microsoft.com/en-us/library/dd460648(v=vs.1 ...
- MEF(Managed Extensibility Framework )的入门介绍
1.什么是MEF MEF是一个来自于微软协作构建扩展应用的新框架,它的目的是在运行中的应用中添加插件.MEF继承于.NET 4.0 Framework平台,存在于各种应用平台的系统程序集中 2.程序集 ...
- 使用Managed Extensibility Framework方便的扩展应用程序
概述 Managed Extensibility Framework(MEF)是.NET平台下的一个扩展性管理框架,它是一系列特性的集合,包括依赖注入(DI)以及Duck Typing等.MEF为开发 ...
- .Net中的插件框架Managed Extensibility Framework
Managed Extensibility Framework(MEF)是微软的一个用来扩展.NET应用程序的框架,它最初为了满足Visual Studio里的编辑器的需求,比如说,延迟加载所有东西和 ...
- 体验Managed Extensibility Framework精妙的设计
MEF(Managed Extensibility Framework)是.NET Framework 4.0一个重要的库,Visual Studio 2010 Code Editor的扩展支持也是基 ...
- 实战MEF(1)一种不错的扩展方式
在过去,我们完成一套应用程序后,如果后面对其功能进行了扩展或修整,往往需要重新编译代码生成新的应用程序,然后再覆盖原来的程序.这样的扩展方式对于较小的或者不经常扩展和更新的应用程序来说是可以接受的,而 ...
- Extjs 5 可选择日期+时间的组件DateTimeField
我们都知道ExtJs有日期组件DateField,但直到ExtJs 5.0版本该日期组件也只能选择日期,不能选择时间(具体到时.分.秒),而实际工作中又常常会有需要日期和时间同时选择的需求,我们只能自 ...
随机推荐
- 关于C语言的几个考试编程题目
提交要求:1:邮件名称:学号后三位-题目编号-姓名-期中考试.例如:098-1-沈苗-期中考试2:不用附件提交,直接写邮件,内容包括编程思路(写一段自己对题目的认识.思路.技术细节等).源代码.运行结 ...
- ueditor初始化
一.下载文件复制到项目中 二.复制表情文件 三.复制列表图片 四.修改ueditor.config.js文件 五.接着修改net文件下config.json文件 六.完蛋了,不支持IE8,版本替换为了 ...
- Linux下安装Zookeeper
Zookeeper是一个协调服务,可以用它来作为配置维护.名字服务.分布式部署: 下面,我来分享一下在Linux下安装Zookeeper的整个步骤,让大家少走弯路. 一.Zookeeper下载 [ro ...
- mysql单表多timestamp报错#1293 - Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
一个表中出现多个timestamp并设置其中一个为current_timestamp的时候经常会遇到#1293 - Incorrect table definition; there can be o ...
- php7的新特性
新增操作符1.??$username = $_GET['user'] ?? '';$username = isset($_GET['user']) ? $_GET['user'] : 'nobody' ...
- Adobe PS CS6安装详解
Adobe PS CS6安装破解详解 注:电脑上是否拥有虚拟光驱,若是没有,推荐2345好压:官网http://haozip.2345.com/下载地址:http://dl.2345.com/haoz ...
- 【小思考】Python的float转换精度损失所想到的
首先,为啥会要讨论这个问题. 我得为昨天拖了小组后腿深表歉意.其实程序逻辑很快就理通了的,但自己总是会因为各种各样的小问题束缚手脚,看接下来这个图片: 稍微有数据敏感性的同学就能看出,中间这么一大堆又 ...
- CentOS 7下MySQL5.7.23的服务配置参数测试
CentOS 7默认安装MySQL5.7.23,服务管理发生了变化,从sysvinit(service mysql start)变化为systemd(systemctl start mysqld.se ...
- perf工具crash的问题
perf抓取时系统crash的情况.找前同事了解到perf工具导致系统crash的一种情况, perf工具默认是使用cycles,这个硬件事件是使用NMI,可能会导致内核错误. 之前文档上的perf命 ...
- SpringMvc和servlet对比
一.servlet实现登录. 咱们先来看一下servlet实现注册登录. <servlet> <servlet-name>LoginServlet</servlet-na ...