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());
} Console.Read();
} private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
}
导入导出方法用到了Func<T>委托,当然没有返回值的话可以用Action<T>委托,关于委托这里就不多说了,大家可以自行百度。
MEF 基础简介 三的更多相关文章
- MEF 基础简介 一
前言 小编菜鸟级别的程序员最近感慨颇多,经历了三五春秋深知程序路途遥远而我沧海一粟看不到的尽头到不了的终点何处是我停留的驿站.说了段废话下面进入正题吧! 什么是MEF? MEF:全称Managed E ...
- MEF 基础简介 四
前言 前面讲解了MEF的引用方法,接口的导入导出,类属性的导入导出和集合的导出用法其实大家可以看到基本上大同小异的. MEF的延迟加载 我们知道当装配一个组件的时候,当前组件里面的所有的Import的 ...
- MEF 基础简介 二
MEF的导出(Export)和导入(Import) using System; using System.Collections.Generic; using System.Linq; using S ...
- 现代3D图形编程学习-基础简介(2) (译)
本书系列 现代3D图形编程学习 基础简介(2) 图形和渲染 接下去的内容对渲染的过程进行粗略介绍.遇到的部分内容不是很明白也没有关系,在接下去的章节中,会被具体阐述. 你在电脑屏幕上看到的任何东西,包 ...
- 现代3D图形编程学习-基础简介(1) (译)
本书系列 现代3D图形编程学习 基础简介 并不像本书的其他章节,这章内容没有相关的源代码或是项目.本章,我们将讨论向量,图形渲染理论,以及OpenGL. 向量 在阅读这本书的时候,你需要熟悉代数和几何 ...
- 这回真的是挤时间了-PHP基础(三)
hi 刚看了唐人街探案,5星好评啊亲.由于是早就约好的,也不好推辞(虽然是和男的..),但该写的还是得挤时间写.明天早上老师的项目结题,虽然和我关系不大,但不要添乱就好!! 1.PHP 一.PHP基 ...
- 1.CSS基础简介
一.基础简介 1.简介 CSS(Cascading Style Sheet)可译为“层叠样式表”或“级联样式表”,它定义如何显示 HTML 元素,用于控制Web页面的外观.通过使用CSS实现页面的内容 ...
- 1.bootstrap基础简介
一·基础简介 1.Bootstrap,来自 Twitter,是一个用于快速开发 Web 应用程序和网站的前端框架,是目前最受欢迎的前端框架. Bootstrap 是基于 HTML.CSS.JavaSc ...
- Android测试基础题(三)
今天接着给大家带来的是Android测试基础题(三). 需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...
随机推荐
- tyflow birth节点
0-50帧,持续出生5颗粒子,若范围为0-0,5颗粒子将在第一帧全部出生 每一帧出生5颗粒子,直到50帧结束 连续发射,在0-500帧范围内,每5颗粒子出生后,继续出生5颗 5颗粒子出生后持续50帧, ...
- 4.21Linux(2)
2019-4-21 22:46:55 今天买了阿里云服务器1年的 116大洋!!! 但是有个服务器感觉很爽!!!!Linux系统还是很有意思的!!!! 直接贴上笔记! 越努力,越幸运!永远不要高估自己 ...
- [LeetCode] Split Array into Fibonacci Sequence 分割数组成斐波那契序列
Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like ...
- 找一个数组的最大和的连续子数组(时间复杂度 O(n))
设计思想 一开始的思想是求出全部的情况,再分别比较大小,这种方法适用于有限个数组,不适用于输入数组长度和内容的情况. 但也试着做了 int a[]= {-1,2,6,-10}; int size=4; ...
- ora-01033 oracle initialization or
这次出现这个问题是源于错删了 DBF文件. 解决方案如下: 1.打开SQL Plus 最后把你删掉的那个文件的表空间删掉就好了
- 使用abcpdf分页设置的问题
如果需要在分页时不对模块进行截断,请为相应模块添加打印样式“page-break-inside: avoid” 如果需要在指定位置进行强制分页,请添加:“<div style="pag ...
- .NET Core多平台开发体验[4]: Docker
对于一个 .NET开发人员,你可能没有使用过Docker,但是你不可能没有听说过Docker.Docker是Github上最受欢迎的开源项目之一,它号称要成为所有云应用的基石,并把互联网升级到下一代. ...
- [Swift]LeetCode969.煎饼排序 | Pancake Sorting
Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...
- C/C++数据在内存中的存储方式
目录 1 内存地址 2 内存空间 在学习C/C++编程语言时,免不了和内存打交道,在计算机中,我们存储有电影,文档,音乐等数据,这些数据在内存中是以什么形式存储的呢?下面做一下简单介绍. 本文是学 ...
- “百度杯”CTF比赛(二月场)-web-writeup
爆破一: 打开网页看到源代码: 根据提示这题就是找变量的值,本想爆破,但不太现实.百度 php获取变量的值 有个超全局数组 $GLOBALS 爆破二: 打开网页看到源代码: 看到了eval() 函数, ...