2009-01-23 76 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