C # Extensible Programming MEF Learning Note (iii): Exporting methods and properties of a class

Source: Internet
Author: User
Tags export class hosting

In front of the introduction and export of several methods, if you are careful to notice that we are exporting the first class, then methods and properties can be exported??? The answer is yes, below is how the MEF exports methods and properties.

Or the previous code, the second article has provided a download link, you can download learning.

First of all, the export properties, because this is relatively simple, and the export class almost, first to look at the code, mainly to see my annotated place, the code in MusicBook.cs is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.componentmodel.composition;namespace mefdemo{   [Export ("Musicbook")] public   class Musicbook: Ibookservice   {      //Export private Property      [Export (typeof (String)]]      private string _privatebookname = "Private Music BookName ";      Export public Properties      [Export (typeof (String)])] publicly      string _publicbookname = "Common Music bookname";      public string BookName {get; set;}   }   [Export ("Mathbook", typeof (Ibookservice))]   public class Mathbook:ibookservice   {public      string BookName {get; set;}      public string Getbookname ()      {         return ' Mathbook ';      }   }   [Export ("Historybook", typeof (Ibookservice))]   public class Historybook:ibookservice   {public      string BookName {get; set;}      public string Getbookname ()      {         return ' Historybook ';      }}   }

The code in Program.cs is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Reflection;using   System.componentmodel.composition;using System.componentmodel.composition.hosting;namespace MEFDemo{Class Program      {[ImportMany ("Mathbook")] public ienumerable<object> Services {get; set;}      Import attributes, which do not distinguish between public or private [ImportMany] public list<string> inputstring {get; set;}         static void Main (string[] args) {Program Pro = new Program ();         Pro.compose (); if (pro. Services! = null) {foreach (var s in pro.               Services) {var ss = (Ibookservice) s; Console.WriteLine (ss.            Getbookname ()); }} foreach (Var str in pro.)         inputstring) {Console.WriteLine (str);      } console.read ();   } private void Compose () {var catalog = new Assemblycatalog (assembly.getexecutingassembly ());      Compositioncontainer container = new Compositioncontainer (catalog);      Container.composeparts (this); }   }}

The following also uses foreach to iterate through the values of the output properties and run to see the results. Finally I will attach the source code for everyone to download, here is no longer.

The following is an export method, the same way whether public or private methods can be exported, the Musicbook code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using      System.componentmodel.composition;namespace mefdemo{[Export ("Musicbook")] public class Musicbook:ibookservice {      Export private Property [Export (typeof (String))] private string _privatebookname = "Private Music bookname";      Export public properties [Export (typeof (String)])] publicly string _publicbookname = "Common Music bookname";      public string BookName {get; set;}      Export the public method [Export (typeof (Func<string>))] The common string Getbookname () {return "Musicbook";         }//Export private method [Export (typeof (Func<int, string>))] private string getbookprice (int price) {      Return "$" + price; }} [Export ("Mathbook", typeof (Ibookservice))] public class Mathbook:ibookservice {public string bookname      {get; set;}      public string Getbookname () {return ' Mathbook '; }} [Export ("Historybook", typeof (Ibookservice)] public class Historybook:ibookservice {public string BookName {get; set;}      public string Getbookname () {return ' Historybook '; }   }}

The code in program is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Reflection;using   System.componentmodel.composition;using System.componentmodel.composition.hosting;namespace MEFDemo{Class Program      {[ImportMany ("Mathbook")] public ienumerable<object> Services {get; set;}      Import attributes, which do not distinguish between public or private [ImportMany] public list<string> inputstring {get; set;}      Import no parameter method [import] public func<string> Methodwithoutpara {get; set;}      Import parameter method [import] public func<int,string> Methodwithpara {get; set;}         static void Main (string[] args) {Program Pro = new Program ();         Pro.compose (); if (pro. Services! = null) {foreach (var s in pro.               Services) {var ss = (Ibookservice) s; Console.WriteLine (ss.            Getbookname ()); }} foreach (Var str in pro.)           InputString) { Console.WriteLine (str); }//Call the parameterless method if (Pro.methodwithoutpara! = null) {Console.WriteLine (Pro.methodwithoutpar         A ()); }//Call the parameter method if (Pro.methodwithpara! = null) {Console.WriteLine (Pro.methodwithpara (3000         ));      } console.read ();         } private void Compose () {var catalog = new Assemblycatalog (assembly.getexecutingassembly ());         Compositioncontainer container = new Compositioncontainer (catalog);      Container.composeparts (this); }   }}

Import and Export method with the Func<t> delegate, of course, there is no return value can be entrusted with action<t>, about the delegation here is not much to say, we can own Baidu.

Click here to download the source code

C # Extensible Programming MEF Learning Note (iii): Exporting methods and properties of a class (go)

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.