2014-04-04 7 views
0

아래 코드에서 일치하는 내보내기와 가져 오기를 일치시키기 위해 MEF를 사용하려고합니다. TestMEFClass에는 일치하는 계약 이름을 공유하는 가져 오기와 내보내기가 있습니다. 내보내기는 호출 될 때마다 카운터를 증가시켜야합니다.내보내기에 일치하는 MEF 가져 오기

내보내기를 콘솔에 인쇄 할 때 카운터가 증가하지 않았습니다. 누군가 내 실수를 지적 할 수 있을까요?

, 당신에게 대단히 감사합니다 내가 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

. 그러나 함수를 사용하는 것이 트릭을 만들었습니다. 이 코드가 다른 사람을 돕기를 바랍니다.

감사합니다,

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

     } 
    } 
} 
관련 문제