I've made a very simple MEF sample which runs on .NET, but doesn't work properly on Mono.
我做了一个非常简单的MEF示例,它运行在.NET上,但在Mono上无法正常工作。
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;
namespace Vialis
{
class Program
{
[Import(typeof(ILedController))]
public List<ILedController> Controllers
{
get;
set;
}
static void Main(string[] args)
{
new Program();
}
public Program()
{
compose();
selectController();
Console.ReadLine();
}
private void compose()
{
var catalog = new DirectoryPartCatalog("controllers");
var container = new CompositionContainer(catalog);
container.AddPart(this);
container.Compose();
}
private void selectController()
{
Console.Clear();
Console.WriteLine("Please select the controller to use\n");
byte i = 0;
foreach (var controller in Controllers)
{
Console.WriteLine("\t{0}) {1}", i, controller.ToString());
i++;
}
Console.Write("\nYour selection: ");
var input = Convert.ToInt32(Console.ReadLine());
Controllers[input].DisplayText(10, 10, "Hello World");
}
}
}
This is the interface:
这是界面:
using System;
using System.Collections.Generic;
using System.Text;
namespace Vialis
{
public interface ILedController
{
void DisplayText(int x, int y, string text);
}
}
This is the first implementation:
这是第一个实现:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;
namespace Vialis
{
[Export(typeof(ILedController))]
public class LyanController : ILedController
{
public void DisplayText(int x, int y, string text)
{
Console.SetCursorPosition(x, y);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(text);
}
}
}
The second implementation:
第二个实现:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.Composition;
namespace Vialis
{
[Export(typeof(ILedController))]
public class VialisController : ILedController
{
public void DisplayText(int x, int y, string text)
{
Console.SetCursorPosition(x, y);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(text);
}
}
}
This is what happens on .NET (Windows):
这是在.NET(Windows)上发生的事情:
Selecting the controllers:
选择控制器:
.NET 1 http://lh3.ggpht.com/_GWubgra2SwM/SXl-yYzs-aI/AAAAAAAADwE/WomfaQqv_Xc/vialis-controller-windows.jpg
.NET 2 http://lh6.ggpht.com/_GWubgra2SwM/SXl-yE1Q-cI/AAAAAAAADwA/qznnEkiNokA/lyan-controller-windows.jpg
But using Mono 2.2 the assemblies don't load:
但是使用Mono 2.2时,程序集不会加载:
Any suggestions ?
有什么建议 ?
INFO: Google seems to be having some picasa web problems, that is why the images don't load.
信息:Google似乎有一些picasa网络问题,这就是图片无法加载的原因。
The pictures show you that on Mac OS, no controllers are listed.
图片显示,在Mac OS上,未列出任何控制器。
3 个解决方案
#1
With the latest MEF release (Preview 4 - http://www.codeplex.com/MEF) it works just fine!
随着最新的MEF发布(预览4 - http://www.codeplex.com/MEF),它的工作正常!
Since the bug is no longer relevant I voted to close this question.
由于这个bug不再相关,我投票决定关闭这个问题。
#2
So I'm assuming that the exports are defined in external assemblies, because you use the DirectoryPartCatalog.. either there is an issue with the file path handling in the catalog or the problem is in the AttributedAssemblyPartCatalog / AttributedTypesPartCatalog
所以我假设导出是在外部程序集中定义的,因为你使用DirectoryPartCatalog ..目录中的文件路径处理存在问题,或者问题出在AttributedAssemblyPartCatalog / AttributedTypesPartCatalog中
Internally the DirectoryPartCatalog wraps each discovered assembly into an AttributedAssemblyPartCatalog which in turn uses a AttributedTypesPartCatalog
DirectoryPartCatalog在内部将每个发现的程序集包装到AttributedAssemblyPartCatalog中,后者又使用AttributedTypesPartCatalog
Your best bet is to add the MEF project into your solution, instead of the dll, and set a break point in the greediest constructors of DirectoryPartCatalog & AttributedAssemblyPartCatalog and step until u find the problem
最好的办法是将MEF项目添加到您的解决方案中,而不是dll中,并在DirectoryPartCatalog和AttributedAssemblyPartCatalog的最贪婪构造函数中设置一个断点,直到找到问题为止
Unfortunatly I do not have a mono machine setup so I can't help more than that
不幸的是,我没有单声道机器设置,所以我无法帮助更多
#3
Adding the interface and the two implementations to the application assembly works. So I'll have debug like you suggested, need to find a decent debugger for mono though.
将接口和两个实现添加到应用程序程序集中。所以我会像你建议的那样进行调试,但需要为单声道找到一个不错的调试器。
#1
With the latest MEF release (Preview 4 - http://www.codeplex.com/MEF) it works just fine!
随着最新的MEF发布(预览4 - http://www.codeplex.com/MEF),它的工作正常!
Since the bug is no longer relevant I voted to close this question.
由于这个bug不再相关,我投票决定关闭这个问题。
#2
So I'm assuming that the exports are defined in external assemblies, because you use the DirectoryPartCatalog.. either there is an issue with the file path handling in the catalog or the problem is in the AttributedAssemblyPartCatalog / AttributedTypesPartCatalog
所以我假设导出是在外部程序集中定义的,因为你使用DirectoryPartCatalog ..目录中的文件路径处理存在问题,或者问题出在AttributedAssemblyPartCatalog / AttributedTypesPartCatalog中
Internally the DirectoryPartCatalog wraps each discovered assembly into an AttributedAssemblyPartCatalog which in turn uses a AttributedTypesPartCatalog
DirectoryPartCatalog在内部将每个发现的程序集包装到AttributedAssemblyPartCatalog中,后者又使用AttributedTypesPartCatalog
Your best bet is to add the MEF project into your solution, instead of the dll, and set a break point in the greediest constructors of DirectoryPartCatalog & AttributedAssemblyPartCatalog and step until u find the problem
最好的办法是将MEF项目添加到您的解决方案中,而不是dll中,并在DirectoryPartCatalog和AttributedAssemblyPartCatalog的最贪婪构造函数中设置一个断点,直到找到问题为止
Unfortunatly I do not have a mono machine setup so I can't help more than that
不幸的是,我没有单声道机器设置,所以我无法帮助更多
#3
Adding the interface and the two implementations to the application assembly works. So I'll have debug like you suggested, need to find a decent debugger for mono though.
将接口和两个实现添加到应用程序程序集中。所以我会像你建议的那样进行调试,但需要为单声道找到一个不错的调试器。