2009-01-23 141 views
2

我做了一个非常简单的MEF示例,它运行在.NET上, 但在Mono上无法正常工作。单声道MEF无法正常工作?

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"); 
     } 
    } 
} 

这是接口:

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace Vialis 
{ 
    public interface ILedController 
    { 
     void DisplayText(int x, int y, string text); 
    } 
} 

这是第一个实现:

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); 
     } 
    } 
} 

第二种方案:

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); 
     } 

    } 
} 

这是.NET会发生什么(Windows):

​​

选择控制器:

.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

但使用单声道2.2的组件不加载:

Mono http://lh5.ggpht.com/_GWubgra2SwM/SXl-xw0YXOI/AAAAAAAADv8/7j2UxJott04/controller-selection-macos.jpg

有什么建议吗?

信息:谷歌似乎有一些picasa网络问题, 这就是为什么图像不加载。

图片显示,在Mac OS上,没有列出控制器。

+0

在我的新闻阅读器图片显示正确(谷歌阅读器),但在本网站上,他们不是...奇数 – TimothyP 2009-01-25 14:59:01

回答

4

随着最新的MEF版本(预览版4 - http://www.codeplex.com/MEF),它工作得很好!

由于错误不再相关,我投票结束了这个问题。

+0

谢谢你让我们知道,蒂莫西。我借调了你的投票。 – 2009-02-01 08:04:08

3

所以我假定出口在外部组件定义,因为您使用DirectoryPartCatalog ..要么有一个与目录或问题的文件路径的处理的一个问题是在AttributedAssemblyPartCatalog/AttributedTypesPartCatalog

内部的DirectoryPartCatalog包装每一个发现的组件插入到AttributedAssemblyPartCatalog这反过来使用AttributedTypesPartCatalog

最好的办法是到MEF项目DirectoryPartCatalog的贪婪的构造函数添加到您的解决方案,而不是DLL,并设置一个断点& AttributedAssemblyPartCatal OG和步骤,直到ü找到问题

不幸的是我没有一个单一的机器设置,所以我不能帮助比

0

更添加接口和两个实现的应用程序组件的工作。 所以我会按照你的建议进行调试,但是需要为单声道找到一个体面的调试器。

+0

我将从传递给DirectoryPartCatalog的路径开始 - 我只有一种感觉,但它确实听起来像是单声道(可能在OSX上)处理路径与Windows上的NETFX不同。 – TheCodeJunkie 2009-01-26 10:24:41