Running MEF on mono
I tried to compile the MEF code copied from this page.
using System.ComponentM开发者_如何转开发odel.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.
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.
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.
精彩评论