namespace MEFDemo
{
class Program
{
private CompositionContainer _container;
[ImportMany(typeof(ICalculator))]
IEnumerable<Lazy<ICalculator>> calculators;
public Program()
{
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
_container = new CompositionContainer(catalog);
try
{
this._container.ComposeParts(this);
}
catch (CompositionException compositionException)
{
Console.WriteLine(compositionException.ToString());
}
}
static void Main(string[] args)
{
Program p = new Program();
string s;
Console.WriteLine("Enter Command:");
while (true)
{
s = Console.ReadLine();
foreach (Lazy<ICalculator> i in p.calculators)
{
Console.WriteLine(i.Value.Calculate(s));
}
}
}
}
public interface ICalculator
{
String Calculate(String input);
}
[Export(typeof(ICalculator))]
class Add : ICalculator
{
string ICalculator.Calculate(string input)
{
return "Symbol=a,输入值" + input;
}
}
[Export(typeof(ICalculator))]
class Add2 : ICalculator
{
string ICalculator.Calculate(string input)
{
return "Symbol=b,输入值" + input;
}
}
}
MEF实例
原创
©著作权归作者所有:来自51CTO博客作者李新雅的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章