MEF和MAF都是C#下的插件编程框架,我们通过它们只需简单的配置下源代码就能轻松的实现插件编程概念,设计出可扩展的程序。这真是件美妙的事情!
MEF(Managed Extensibility Framework)
MEF的工作原理大概是这样的:首先定义一个接口,用这个接口来约束插件需要具备的职责;然后在实现接口的程序方法上面添加反射标记“[Export()]”将实现的内容导出;最后在接口的调用程序中通过属性将插件加载进来。我们还是用代码来描述吧:
1. 定义一个接口:
/* 作者:GhostBear 博客:http://blog.csdn.net/ghostbear 简介:该节主要学习.net下的插件编程框架MEF(managed extensibility framework) */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; namespace chapter28_simplecontract { public interface ICalculator { IList<IOperation> GetOperations(); double Operate(IOperation operation, double[] operands); } public interface IOperation { string Name { get; } int NumberOperands { get; } } public interface ICaculatorExtension { string Title { get; } string Description { get; } FrameworkElement GetUI(); } }
2. 实现定义的接口(部分一)
[Export(typeof(ICalculator))] public class Caculator:ICalculator { public IList<IOperation> GetOperations() { return new List<IOperation>(){ new Operation{ Name="+",NumberOperands=2}, new Operation{Name="-",NumberOperands=2}, new Operation{Name="*",NumberOperands=2}, new Operation{Name="/",NumberOperands=2} }; } public double Operate(IOperation operation, double[] operands) { double result=0; switch (operation.Name) { case "+": result = operands[0] + operands[1]; break; case "-": result = operands[0] - operands[1]; break; case "*": result = operands[0] * operands[1]; break; case "/": result = operands[0] / operands[1]; break; default: throw new Exception("not provide this method"); } return result; } } public class Operation:IOperation { public string Name { get; internal set; } public int NumberOperands { get; internal set; } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索接口
, 插件
, public
, result
, name
, operation
, windows框架插件net
, 插件框架 osgi mef
, getui
插件编程
c站、c语言、cf、ch、c罗,以便于您获取更多的相关知识。