Both MEF and MAF are the plug-in programming frameworks under C #, and we can easily implement the plug-in programming concept by simply configuring the source code and designing an extensible program. This is a wonderful thing!
MEF (Managed extensibility Framework)
The way MEF works is probably this: first, define an interface to use this interface to constrain the responsibilities that the plug-in needs to have; then add the Reflection Flag "[Export ()]" to the program method that implements the interface to export the implemented content, and finally load the plug-in into the interface's calling program through the properties. Let's use code to describe it:
1. Define an interface:
/*
Author: ghostbear
Blog: http://blog.csdn.net/ghostbear
Introduction: This section is mainly to learn about the plug-in programming framework under. 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. Implementation-defined interface (PART I)
[Export (typeof (ICalculator))] public class Caculator:icalculator {public ilist&
Lt;ioperation> getoperations () {return new list<ioperation> () { New operation{Name= "+", numberoperands=2}, New Operation{name= "-", numberoperands=2} , New Operation{name= "*", numberoperands=2}, New Operation{name= "/", Numbe
roperands=2}}; Public double Operate (ioperation operation, double[] operands) {DOUBL
e result=0; Switch (operation. Name {case "+": result = Operands[0] + Opera
NDS[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; }
}