{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
{
void GetBookName();
}
/// <summary>
/// 导入
/// </summary>
[Export(typeof(IBookService))]
public class ComputerBookService : IBookService
{
public void GetBookName()
{
Console.WriteLine("《Hello Silverlight》");
}
}
/// 导入接口的实现部件(Part)
/// </summary>
[Import]
public IBookService Service { get; set; }
/// 宿主MEF并组合部件
/// </summary>
private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
//将部件(part)和宿主程序添加到组合容器
container.ComposeParts(this,new ComputerBookService());
}
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();
}
}
}