MEF 基础简介 三

Posted smileiven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MEF 基础简介 三相关的知识,希望对你有一定的参考价值。

 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>委托,关于委托这里就不多说了,大家可以自行百度。

以上是关于MEF 基础简介 三的主要内容,如果未能解决你的问题,请参考以下文章

C#可扩展编程之MEF学习笔记:MEF简介及简单的Demo(转)

Android 逆向加壳技术简介 ( 动态加载 | 第一代加壳技术 - DEX 整体加固 | 第二代加壳技术 - 函数抽取 | 第三代加壳技术 - VMP / Dex2C | 动态库加壳技术 )

MEF:“可组合部件应包含至少一个导出”

csharp MEF基础知识

C#可扩展编程之MEF:MEF高级进阶

验证码逆向专栏极验三代四代点选类验证码逆向分析