Plug-in programming framework under C #: MEF and MAF

Source: Internet
Author: User

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; }  
      
    }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.