大家好,欢迎来到IT知识分享网。
C#进阶系列
第一章 【C#进阶系列】【MEF框架(一)】
前言
这里对MEF作了基本的介绍,包括使用了一个特定场景(搞自动化运控上位机开发的应该更容易代入场景了),一步一步地介绍了如果从常用的编程过渡到框架性的编程开发。
一、MEF介绍
- MEF,全称Managed Extensibility Framework(托管可扩展框架)。MEF是专门致力于解决扩展性问题的框架。
- MSDN:Managed Extensibility Framework 或 MEF 是一个用于创建可扩展的轻型应用程序的库。 应用程序开发人员可利用该库发现并使用扩展,而无需进行配置。 扩展开发人员还可以利用该库轻松地封装代码,避免生成脆弱的硬依赖项。 通过 MEF,不仅可以在应用程序内重用扩展,还可以在应用程序之间重用扩展。
- MEF(Managed Extensibility Framework),是轻量级的插件框架。使用简单,功能强大。
- MEF 位于 ComponentModel.Composition 程序集中,添加 System.ComponentModel.Composition 和 System.ComponentModel.Composition.Hosting 的项目引用
二、为什么要用MEF
class Program {
static void Main(string[] args) {
string ret = ""; string cardName = "实际设备使用哪种运动控制卡"; if (cardName == "固高板卡") {
CLeadshine cLeadshine = new CLeadshine(); ret = cLeadshine.InitMotionCard(); ret = cLeadshine.ExitMotionCard(); } else if (cardName == "雷赛板卡") {
CGoogol cGoogol = new CGoogol(); ret = cGoogol.InitMotionCard(); ret = cGoogol.ExitMotionCard(); } } } public class CLeadshine {
public string InitMotionCard() {
return "初始化固高板卡成功"; } public string ExitMotionCard() {
return "退出固高板卡成功"; } } public class CGoogol {
public string InitMotionCard() {
return "初始化雷赛板卡成功"; } public string ExitMotionCard() {
return "退出雷赛板卡成功"; } }
class Program {
static void Main(string[] args) {
string ret = ""; string cardName = "实际设备使用哪种运动控制卡"; IMotionCard motionCard = null; if (cardName == "固高板卡") {
motionCard = new CLeadshine(); } else if (cardName == "雷赛板卡") {
motionCard = new CGoogol(); } ret = motionCard.InitMotionCard(); ret = motionCard.ExitMotionCard(); } } public interface IMotionCard {
string InitMotionCard(); string ExitMotionCard(); } public class CLeadshine : IMotionCard {
public string InitMotionCard() {
return "初始化固高板卡成功"; } public string ExitMotionCard() {
return "退出固高板卡成功"; } } public class CGoogol : IMotionCard {
public string InitMotionCard() {
return "初始化雷赛板卡成功"; } public string ExitMotionCard() {
return "退出雷赛板卡成功"; } }
接下来我们开始引入MEF的概念,看看MEF是如何操作的,相比较而言是否实现了松耦合,有哪些实际应用的优势。
三、MEF的概念
四、使用示例
class Program {
static string DllFilePath = ""; static string ContractName = ""; static void Main(string[] args) {
string ret = ""; string cardName = "实际设备使用哪种运动控制卡"; cardName = "固高板卡"; if (cardName == "固高板卡") {
DllFilePath = "Googol.dll"; ContractName = "GoogolCard"; } else if (cardName == "雷赛板卡") {
DllFilePath = "Leadshine.dll"; ContractName = "LeadshineCard"; } ret = InitMotionCard(); ret = ExitMotionCard(); } public static string InitMotionCard() {
return ExportPart(ContractName, DllFilePath).InitMotionCard(); } public static string ExitMotionCard() {
return ExportPart(ContractName, DllFilePath).ExitMotionCard(); } public static IMotionCard ExportPart(string ContractName, string DllName) {
// 创建一个程序集目录,用于从一个程序集获取所有的组件定义 //Assembly.GetExecutingAssembly():当前方法所在程序集 //Assembly.GetCallingAssembly():调用当前方法的方法 所在的程序集 //Assembly.LoadFrom(@"DLLName.dll"):DLLName.dll的程序集 AssemblyCatalog Catalog = new AssemblyCatalog(Assembly.LoadFrom(DllName)); //创建容器 CompositionContainer Container = new CompositionContainer(Catalog); //获得容器中的Export IMotionCard Export = Container.GetExportedValue<IMotionCard>(ContractName); return Export; } }
插件MotionCard.dll
public interface IMotionCard {
string InitMotionCard(); string ExitMotionCard(); }
插件Googol.dll
[Export("GoogolCard", typeof(IMotionCard))] public class CGoogol : IMotionCard {
public string InitMotionCard() {
return "初始化固高板卡成功"; } public string ExitMotionCard() {
return "退出固高板卡成功"; } }
插件Leadshine.dll
[Export("LeadshineCard", typeof(IMotionCard))] public class CLeadshine : IMotionCard {
public string InitMotionCard() {
return "初始化雷赛板卡成功"; } public string ExitMotionCard() {
return "退出雷赛板卡成功"; } }
五、MEF框架的好处
解耦
试想下,如果主程序引用了Leadshine.dll和Googol.dll,那么就意味着你可以无限制的开放DLL中类,属性,方法的访问权限,也就意味着主程序中会出现很多耦合的代码,哪天你想移除这个DLL,程序肯定编译失败,而且你要手动删除这些耦合代码,而MEF因为是通过中间接口来完成调用的,所以只向外暴露了接口里面的成员,程序员是无法任意调用DLL中的任何方法,只能通过接口来调用。就算删掉这个DLL,程序也能正常运行!
可扩展性
举个例子,假设你的程序已经移交给客户了,哪天客户说我不想用固高、雷赛的板卡了,我要用正运动的板卡,这时,你只需重新一个ZMotion.dll,使其继承并实现IMotionCard接口,然后,只要将ZMotion.dll和改好的配置文件交给客户,并让其修改选择选项,就能运行使用正运动板卡了。是不是很方便!如果是以前,必须要修改主程序的代码,然后重新编译,发布,再将整个程序移交客户,这样说大家应该都明白了!
更多
MEF不仅可以导出类,还可以导出方法,属性,不管是私有还是公有,从而满足更多的需求!
六、源码链接
MEF插件框架学习展示(源代码)(一)
总结
依赖倒置原则:高层模板不应该依赖于底层模板,两者应该依赖于抽象,而抽象不应该依赖于细节。
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/140040.html