概要
导出
[Export(typeof(ICalculator))]
public class Calculator:ICalculator
导入
[Import(typeof(ICalculator))]
public Lazy<ICalculator> Calculator { get; set; }
入口和出口映射
var catalog = new DirectoryCatalog(@"D:\projects\工程试验\计算\bin\Debug");
var container = new CompositionContainer(catalog);
try
{
container.ComposeParts(this);
int a = Calculator.Value.add(5, 6);
代码-第一版 下载
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MEF_简单例子
{
class Program
{
//导入,使用默认的ContractType和ContractName
[Import]
public ILog log
{
get;
set;
}
static void Main(string[] args)
{
Program pro = new Program();
pro.Compose();
pro.log.AddLog(new Exception("简单MFC试验"));
Console.Read();
}
//组合方法
private void Compose()
{
//指定目录为当前执行的程序集
var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
//使用AssemblyCatalog创建组合容器
var container = new CompositionContainer(catalog);
//调用组合部件方法
container.ComposeParts(this);
}
}
//定义日志接口
public interface ILog
{
void AddLog(Exception ex);
}
//导出部件,指定协定类型(ContractType)为ILog
[Export(typeof(ILog))]
public class MyLog : ILog
{
public void AddLog(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
代码-第二版
工程结构
导入文件放置
代码
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MEF_简单例子
{
class Program
{
//导入,使用默认的ContractType和ContractName
[Import]
public ILog log
{
get;
set;
}
static void Main(string[] args)
{
Program pro = new Program();
pro.Compose();
pro.log.AddLog(new Exception("简单MFC试验"));
Console.Read();
}
//组合方法
private void Compose()
{
//指定目录为当前执行的程序集
var catalog = new DirectoryCatalog(@"C:\AddIns2");
//使用AssemblyCatalog创建组合容器
var container = new CompositionContainer(catalog);
//调用组合部件方法
container.ComposeParts(this);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MEF_简单例子
{
public interface ILog
{
void AddLog(Exception ex);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MEF_简单例子
{
//导出部件,指定协定类型(ContractType)为ILog
[Export(typeof(ILog))]
public class MyLog : ILog
{
public void AddLog(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
运行效果
代码-第三版
代码
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wrox.ProCSharp.MEF
{
class Program
{
private CalculatorImport calcImport;
static void Main(string[] args)
{
Console.WriteLine();
Program program = new Program();
program.run();
Console.ReadLine();
}
public void run()
{
//"C:\AddIns"
var catalog = new DirectoryCatalog(@"D:\projects\工程试验\计算\bin\Debug");
//var catalog = new DirectoryCatalog(@"C:\AddIns3");
var container = new CompositionContainer(catalog);
try
{
calcImport = new CalculatorImport();
calcImport.ImportsSatisfied += (sender, e) =>
{
//textStatus.Text += String.Format("{0}\n", e.StatusMessage);
Console.WriteLine(e.StatusMessage);
};
// container.ComposeParts(calcImport);
var batch = new CompositionBatch();
batch.AddPart(calcImport);
container.Compose(batch);
int a = calcImport.Calculator.Value.add(5, 6);
Console.WriteLine(a);
}
catch (ChangeRejectedException ex)
{
Console.WriteLine(ex.Message);
return;
}
}
}
public class CalculatorImport : IPartImportsSatisfiedNotification
{
public event EventHandler<ImportEventArgs> ImportsSatisfied;
[Import(typeof(ICalculator))]
public Lazy<ICalculator> Calculator { get; set; }
public void OnImportsSatisfied()
{
if (ImportsSatisfied != null)
ImportsSatisfied(this, new ImportEventArgs { StatusMessage = "ICalculator import successful" });
}
}
public class ImportEventArgs : EventArgs
{
public string StatusMessage { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wrox.ProCSharp.MEF
{
public interface ICalculator
{
int add(int a, int b);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wrox.ProCSharp.MEF
{
[Export(typeof(ICalculator))]
public class Calculator:ICalculator
{
int ICalculator.add(int a, int b)
{
return a + b;
}
}
}
代码-第四版
代码
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace Wrox.ProCSharp.MEF
{
class Program
{
[Import(typeof(ICalculator))]
public Lazy<ICalculator> Calculator { get; set; }
static void Main(string[] args)
{
Console.WriteLine();
Program program = new Program();
program.run();
Console.ReadLine();
}
public void run()
{
var catalog = new DirectoryCatalog(@"D:\projects\工程试验\计算\bin\Debug");
//var catalog = new DirectoryCatalog(@"C:\AddIns3");
var container = new CompositionContainer(catalog);
try
{
container.ComposeParts(this);
int a = Calculator.Value.add(5, 6);
Console.WriteLine(a);
}
catch (ChangeRejectedException ex)
{
Console.WriteLine(ex.Message);
return;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wrox.ProCSharp.MEF
{
public interface ICalculator
{
int add(int a, int b);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wrox.ProCSharp.MEF
{
[Export(typeof(ICalculator))]
public class Calculator:ICalculator
{
int ICalculator.add(int a, int b)
{
return a + b;
}
}
}