在应用程序中宿主MEF其实非常简单,只需要创建一个组合容器对象(CompositionContainer)的实例,然后将需要组合的部件(Parts)和当前宿主程序添加到容器中即可。首先需要添加MEF框架的引用,既System.ComponentModel.Composition.dll,详细如下代码块:
private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
通过上面的代码实现就可以完成MEF的宿主,实际上在使用MEF的开发过程中并不会如此简单的应用。可能会定义一个或多个导入(Import)和导出(Export)部件,然后通过MEF容器进行组合,其实也可以理解为“依赖注入”的一种实现。比如定义一个图书接口和一个接口的实现类,在此基础上使用MEF的导入导出特性:
public interface IBookService
{
void GetBookName();
}
/// <summary>
/// 导入
/// </summary>
[Export(typeof(IBookService))]
public class ComputerBookService : IBookService
{
public void GetBookName()
{
Console.WriteLine("《Hello Silverlight》");
}
}
{
void GetBookName();
}
/// <summary>
/// 导入
/// </summary>
[Export(typeof(IBookService))]
public class ComputerBookService : IBookService
{
public void GetBookName()
{
Console.WriteLine("《Hello Silverlight》");
}
}
如上代码通过使用MEF的[System.ComponentModel.Composition.Export]对接口的实现进行导出设置,让接口的实现以容器部件的方式存在,然后通过组合容器进行装配加载,这个过程中就包括了接口的实例化的过程。接下来就需要在MEF的宿主程序中定义一个接口的属性,并为其标注[System.ComponentModel.Composition.Import]特性以实现接口实现类的导入。如下代码块:
/// <summary>
/// 导入接口的实现部件(Part)
/// </summary>
[Import]
public IBookService Service { get; set; }
/// 导入接口的实现部件(Part)
/// </summary>
[Import]
public IBookService Service { get; set; }
完成了导入导出的接口与实现的开发及特性配置,下面就剩下一步组合了,也就是本文提到的将部件和宿主程序自身添加到组合容器中,以实现导入(Import)和导出(Export)的组合装配。
/// <summary>
/// 宿主MEF并组合部件
/// </summary>
private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
//将部件(part)和宿主程序添加到组合容器
container.ComposeParts(this,new ComputerBookService());
}
/// 宿主MEF并组合部件
/// </summary>
private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
//将部件(part)和宿主程序添加到组合容器
container.ComposeParts(this,new ComputerBookService());
}
通过以上步骤就完成了MEF的宿主以及一个简单的部件组合的应用示例,下面是本文的完整代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace HostingMef
{
public interface IBookService
{
void GetBookName();
}
/// <summary>
/// 导入
/// </summary>
[Export(typeof(IBookService))]
public class ComputerBookService : IBookService
{
public void GetBookName()
{
Console.WriteLine("《Hello Silverlight》");
}
}
class Program
{
/// <summary>
/// 导入接口的实现部件(Part)
/// </summary>
[Import]
public IBookService Service { get; set; }
/// <summary>
/// 宿主MEF并组合部件
/// </summary>
private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
//将部件(part)和宿主程序添加到组合容器
container.ComposeParts(this,new ComputerBookService());
}
static void Main(string[] args)
{
Program p = new Program();
p.Compose();
p.Service.GetBookName();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace HostingMef
{
public interface IBookService
{
void GetBookName();
}
/// <summary>
/// 导入
/// </summary>
[Export(typeof(IBookService))]
public class ComputerBookService : IBookService
{
public void GetBookName()
{
Console.WriteLine("《Hello Silverlight》");
}
}
class Program
{
/// <summary>
/// 导入接口的实现部件(Part)
/// </summary>
[Import]
public IBookService Service { get; set; }
/// <summary>
/// 宿主MEF并组合部件
/// </summary>
private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
//将部件(part)和宿主程序添加到组合容器
container.ComposeParts(this,new ComputerBookService());
}
static void Main(string[] args)
{
Program p = new Program();
p.Compose();
p.Service.GetBookName();
}
}
}