2014-04-04 93 views
0

在下面的代碼中,我嘗試使用MEF將導入匹配到匹配的導出。 TestMEFClass具有共享匹配合同名稱的導入和導出。每次調用時,導出應該增加一個計數器。匹配MEF導入到出口

當我將輸出打印到控制檯時,它沒有增加計數器。有人能指出我的錯誤嗎?

非常感謝你,

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 
using System.ComponentModel.Composition; 
using System.ComponentModel.Composition.Hosting; 
using System.Reflection; 

namespace MEFConsoleTest { 

    public class TestMEFClass { 

     /// <summary> 
     /// This counter should increment everytime the getter in the ExportString property gets called. 
     /// </summary> 
     private int counter = 0; 

     [Export("Contract_Name")] 
     public string ExportString { 

      get { 
       return "ExportString has been called " + counter++.ToString(); 
      } 
     } 

     [Import("Contract_Name")] 
     public string ImportString { get; set; } 

     /// <summary> 
     /// Default Constructor. 
     /// Make a catalog from this assembly, add it to the container and compose the parts. 
     /// </summary> 
     public TestMEFClass() { 

      AggregateCatalog catalog = new AggregateCatalog(); 
      catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); 
      var container = new CompositionContainer(catalog); 
      container.ComposeParts(this); 
     } 


    } 

    class Program { 

     static void Main(string[] args) { 

      TestMEFClass testClass = new TestMEFClass(); 
      Console.WriteLine(testClass.ImportString); 
      Console.WriteLine(testClass.ImportString); 
      Console.ReadLine(); 

     } 
    } 

回答

0

的原因,我不能在這一刻解釋我是不是能夠得到MEF和屬性導入/導出到一個可變的特性工作。但是,使用函數做到了這一點。我希望這段代碼能夠幫助別人。

感謝,

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ComponentModel; 
using System.ComponentModel.Composition; 
using System.ComponentModel.Composition.Hosting; 
using System.Reflection; 

namespace MEFConsoleTest { 

    public class TestMEFClass { 

     /// <summary> 
     /// This counter should increment everytime the getter in the ExportString property gets called. 
     /// </summary> 
     private int counter = 0; 

     [Export("Contract_Name")] 
     string ExportMethod() { 
      return ExportString; 
     } 


     public string ExportString { 

      get { 
       return "ExportString has been called " + counter++.ToString(); 
      } 
     } 

     [Import("Contract_Name")] 
     Func<string> ImportMethod; 

     public string ImportString { get { return ImportMethod(); } } 

     /// <summary> 
     /// Default Constructor. 
     /// Make a catalog from this assembly, add it to the container and compose the parts. 
     /// </summary> 
     public TestMEFClass() { 

      AggregateCatalog catalog = new AggregateCatalog(); 
      catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly())); 
      var container = new CompositionContainer(catalog); 
      container.ComposeParts(this); 
     } 


    } 

    class Program { 

     static void Main(string[] args) { 

      TestMEFClass testClass = new TestMEFClass(); 

      for (int x = 0; x < 10; x++) { 
       Console.WriteLine(testClass.ImportString); 
      } 

      Console.ReadLine(); 

     } 
    } 
}