欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MEF 基础简介 三

程序员文章站 2022-03-26 10:28:23
MEF导出类的方法和属性 首先来说导出属性,因为这个比较简单,和导出类差不多,先来看看代码,主要看我加注释的地方,MusicBook.cs中的代码如下: program.cs中的代码如下: 下面还用foreach遍历输出属性的值,运行即可查看到结果。最后我会附上源码供大家下载,这里就不再截图了。 下 ......

 mef导出类的方法和属性

首先来说导出属性,因为这个比较简单,和导出类差不多,先来看看代码,主要看我加注释的地方,musicbook.cs中的代码如下:

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(typeof(string))]
      private string _privatebookname = "private music bookname";

      //导出公有属性
      [export(typeof(string))]
      public string _publicbookname = "public 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";
      }
   }

}

program.cs中的代码如下:

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

      //导入属性,这里不区分public还是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);
      }
   }
}

下面还用foreach遍历输出属性的值,运行即可查看到结果。最后我会附上源码供大家下载,这里就不再截图了。

下面说导出方法吧,同理无论是公有方法还是私有方法都是可以导出的,musicbook代码如下:

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(typeof(string))]
      private string _privatebookname = "private music bookname";

      //导出公有属性
      [export(typeof(string))]
      public string _publicbookname = "public music bookname";


      public string bookname { get; set; }

      //导出公有方法
      [export(typeof(func<string>))]
      public string getbookname()
      {
         return "musicbook";
      }

      //导出私有方法
      [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";
      }
   }

}

program中的代码如下:

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

      //导入属性,这里不区分public还是private
      [importmany]
      public list<string> inputstring { get; set; }

      //导入无参数方法
      [import]
      public func<string> methodwithoutpara { get; set; }

      //导入有参数方法
      [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);
         }

         //调用无参数方法
         if (pro.methodwithoutpara != null)
         {
            console.writeline(pro.methodwithoutpara());
         }
         //调用有参数方法
         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);
      }
   }
}

导入导出方法用到了func<t>委托,当然没有返回值的话可以用action<t>委托,关于委托这里就不多说了,大家可以自行百度。