前面说完了导入和导出的几种方法,如果大家细心的话会注意到前面我们导出的都是类,那么方法和属性能不能导出呢???答案是肯定的,下面就来说下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());
} 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

C#可扩展编程之MEF学习笔记(二):MEF的导出(Export)和导入(Import)

C#可扩展编程之MEF学习笔记(三):导出类的方法和属性

C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻

C#可扩展编程之MEF学习笔记(五):MEF高级进阶

C#可扩展编程之MEF学习笔记(三):导出类的方法和属性的更多相关文章

  1. C#可扩展编程之MEF学习笔记(五):MEF高级进阶

    好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...

  2. C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻

    前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...

  3. C#可扩展编程之MEF学习笔记(二):MEF的导出(Export)和导入(Import)

    上一篇学习完了MEF的基础知识,编写了一个简单的DEMO,接下来接着上篇的内容继续学习,如果没有看过上一篇的内容, 请阅读:http://www.cnblogs.com/yunfeifei/p/392 ...

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

    在文章开始之前,首先简单介绍一下什么是MEF,MEF,全称Managed Extensibility Framework(托管可扩展框架).单从名字我们不难发现:MEF是专门致力于解决扩展性问题的框架 ...

  5. C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻(转)

    前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...

  6. C#可扩展编程之MEF学习笔记(五):MEF高级进阶(转)

    好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四篇,MEF中比较常用 ...

  7. C#可扩展编程之MEF学习

    MEF系列文章: C#可扩展编程之MEF学习笔记(一):MEF简介及简单的Demo C#可扩展编程之MEF学习笔记(二):MEF的导出(Export)和导入(Import) C#可扩展编程之MEF学习 ...

  8. DirectSound学习(三)--类、方法、属性翻译

    DirectSound.Device :Contains methods and properties used to create buffer objects, manage devices, a ...

  9. C#可扩展编程之MEF

    C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻 前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在 ...

随机推荐

  1. ceph实践: 搭建环境

    作者:吴香伟 发表于 2014/09/26 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 本节主要参考官网的ADDING/REMOVING OSDS章节. 同步 ...

  2. 大漠绑定测试工具-VB6

    获取更新开始|版本:3.1652版 2016年12月27日|更新内容:1.取消自动更新错误的提示.\n\n友情提示:如网盘失效,请加QQ群(568073679)下载最新版|下载地址:http://ww ...

  3. Could not create the view: An unexpected exception was thrown 【转】

    转:http://blog.csdn.net/shuangzixing520/article/details/35225105 今天打开Myeclipse10的时候,发现server窗口出现一堆问题, ...

  4. 安装Openfile环境和组件XMPP

    打开ProFTPD和Apache Web Server 安装Openfile 在系统偏好设置中打开Openfile,开启Openfile状态 在safair浏览器中输入127.0.0.1:9090 配 ...

  5. maven SpringMVC easyUI项目创建

    在Eclipse中使用Maven创建SpringMVC项目,项目所需软件及工具可以在官网下载.Maven.Nexus及Eclipse集成Maven等到此配置完毕. 1.Maven创建Web项目. 打开 ...

  6. AD帐户操作C#示例代码(一)——导入用户信息

    最近写了一个AD帐户导入的小工具(为啥写作“帐”户呢?),跟大家分享下相关代码,欢迎各位高手指教! 首先,我准备一个这样的Excel文件作为导入模版,并添加了一些测试数据. 然后,我打开Visual ...

  7. C# "=="、Equals()、ReferenceEquals()区别

    对于值类型: ; ; 1.== 比较的是值内容 2.age2.Equals(age1) = false; Equals比较前需要转换成同类型,age1(int型)需显示转换成byte型 3.age1. ...

  8. vagrant 基本配置

    首先安装好virtualbox,可以对照官网教程 https://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on- ...

  9. [转载]Firebird与MySQL:一个使用者的体会

    老板要我开发一个LINUX平台上的数据库项目,要求一定要用开源免费数据库.我知道这个数据库必须能够上网操作,同时作为公司的核心骨干数据库,除了必须是稳定的存储数据库外还必须有很强的数据和数据库控管功能 ...

  10. C# Exception 写入文件

    /// <summary> /// 将异常打印到LOG文件 /// </summary> /// <param name="ex">异常< ...