文章中的MEF问题
我在此页面中遇到一个小问题:
http://mef.codeplex.com/ wikipage?title=Parts&referringTitle=Guide
我有这个程序:
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System;
public class Program
{
[Import]
public IMessageSender MessageSender { get; set; }
public static void Main(string[] args)
{
Program p = new Program();
p.Run();
}
public void Run()
{
Compose();
MessageSender.Send("Message Sent");
}
private void Compose()
{
AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
public interface IMessageSender
{
void Send(string message);
}
[Export(typeof(IMessageSender))]
public class EmailSender : IMessageSender
{
public void Send(string message)
{
Console.WriteLine(message);
}
}
而且我不明白如何编辑它以与此代码一起使用:
[Export(typeof(IMessageSender))]
public class EmailSender : IMessageSender {
...
}
[Export(typeof(IMessageSender))]
public class TCPSender : IMessageSender {
...
}
public class Notifier {
[ImportMany]
public IEnumerable<IMessageSender> Senders {get; set;}
public void Notify(string message) {
foreach(IMessageSender sender in Senders)
sender.Send(message);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您所期望的是这样的:
由此,您可以
[Import]
一个INotifier
的实例:尽管如此,我确实更喜欢构造函数注入,因为它更好地表达了依赖关系对于你的班级:
I think what you are expecting, is something like:
Whereby, you can
[Import]
an instance ofINotifier
:Although, I do prefer constructor injection, as it better expresses dependencies for your class:
该示例是为了解释指定接口或抽象类作为导出类型的可能性,即使这也是您的第一个示例中所做的事情。这与导出具体类型不同,例如 [Export(typeof(EMailSender))] 所做的,因为您的导入类必须只知道契约,而不是类的真实名称。此外,它还向您展示了通过 ImportMany 属性导入实现接口的所有导出类型的方法。要在代码中使用它,请使其尽可能简单,即不按照 Matthew 建议引入另一个导入级别或构造函数注入(请查看 http://mef.codeplex.com/wikipage?title=Declaring%20Imports&referringTitle=Guide)您可以简单地在您的 Program 类中修改这些方法:
这应该执行的 Send 方法程序集中 IMessageSender 的所有不同实现。
The example is there to explain the possibility to specify an interface or an abstract class as the exported type, even if this is something that is done in your first example, too. This is different from exporting a concrete type, like [Export(typeof(EMailSender))] would have done, since your importing class must know only the contract, not the real name of the class/classes. Furthermore it shows you the way to import all the exported types implementing your interface, through the ImportMany attribute. To use it in your code keeping it as simple as possible, i.e. without introducing another import level or constructor injection as suggested by Matthew (take a look at http://mef.codeplex.com/wikipage?title=Declaring%20Imports&referringTitle=Guide) you could simply modify these methods in your Program class:
This should execute the Send method of all the different implementation of IMessageSender in your assembly.