在单声道上运行 MEF
我尝试编译从 此页面复制的 MEF 代码。
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);
}
}
当我运行此命令 dmcs example.cs
时,我收到此错误消息。
ex.cs(1,29): error CS0234: The type or namespace name `Composition' does not exist in the namespace `System.ComponentModel'. Are you missing an assembly reference?
我需要提供什么参考库才能删除此错误消息?
添加了
dmcs ex.cs /r:System.ComponentModel.Composition.dll 有效。人们不必从 codeplex 下载 dll。
I tried to compile the MEF code copied from this page.
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);
}
}
When I run this command dmcs example.cs
I got this error message.
ex.cs(1,29): error CS0234: The type or namespace name `Composition' does not exist in the namespace `System.ComponentModel'. Are you missing an assembly reference?
What reference library do I need to give to remove this error message?
ADDED
dmcs ex.cs /r:System.ComponentModel.Composition.dll
works. One doesn't have to download the dlls from codeplex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 MEF 是否包含在 Mono 中不是问题......但您需要 MEF 库(不包含在 Mono 库中)。从 Codeplex 下载该库。然后从您的项目中引用它。如果可行,那么 Mono 可能会很好地使用它。
Whether or not MEF is included in Mono isn't the issue, I think ... but that you need the MEF library (not included in Mono libraries). Download the library from Codeplex. Then reference it from your project. If that works, then Mono may play nice with it.
据我了解,MEF 目前还不是 Mono 的一部分。 Mono 与 AddIn 具有“相似”的构造,但与 MEF 不同。此时,在 Mono 中创建 MEF 就意味着推出您自己的 MEF,除非我错过了一些新的魔法。
Per my understanding, MEF is not part of Mono at this time. Mono has a "similar" construct with AddIn, but it is not identical to MEF. At this time, creating MEF in Mono would mean rolling your own, unless there is some new magic I am missing out on.