2010-06-10 5 views
0

이 질문에 이미 100 번이나 물어 본 점은 죄송하지만 실제로 작동 시키려면 고심하고 있습니다.MEF를 사용하여 엑스포트 된 개체로 가져 오기

3 가지 프로젝트가 있습니다.

  • Core.dll
    • 공통 인터페이스
  • Shell.exe
    • 로드 조립체 폴더에있는 모든 모듈을 갖는다.
    • 참조는
  • ModuleA.dll
    • 이름, 모듈의 버전을 내 보냅니다 Core.dll.
    • 참조는

Shell.exe가있다 Core.dll 나는 모든로드 된 모듈에 주입해야하는 타사 응용 프로그램의 단일 인스턴스를 포함하는 [내보내기].

지금까지 내가 Shell.exe에있는 코드는 :

static void Main(string[] args) 
{ 
     ThirdPartyApp map = new ThirdPartyApp(); 

     var ad = new AssemblyCatalog(Assembly.GetExecutingAssembly()); 
     var dircatalog = new DirectoryCatalog("."); 
     var a = new AggregateCatalog(dircatalog, ad); 

     // Not to sure what to do here. 
} 

class Test 
{ 
    [Export(typeof(ThirdPartyApp))] 
    public ThirdPartyApp Instance { get; set; } 

    [Import(typeof(IModule))] 
    public IModule Module { get; set; } 
} 

내가 다음 ModuleA.dll에서 모듈을로드 Main 방법에서 map와 테스트의 경우,로드 Instance을 작성해야 실행 디렉터리에있는 다음 [가져 오기] Instance로드 된 모듈에. 난 그냥의 인스턴스에 주로 하중까지 시험으로, 모두 함께 넣어하는 방법을 모르겠어요 내가 거기 절반 방법이야 알고

[Export(IModule)] 
class Module : IModule 
{ 
    [Import(ThirdPartyApp)] 
    public ThirdPartyApp Instance {get;set;} 
} 

:

ModuleA에서 나는이 같은 클래스가 map에서 Main.

아무도 도와 줄 수 있습니까?

답변

0

내가 그것을 테스트 클래스의 생성자에서,이 방법을 작동시킬 수있을 것 (예 내가 생성자에서 일을하지 알고, 나는 그것을 밖으로 이동합니다) :

public Test() 
    { 
     ThirdPartyApp map = new ThirdPartyApp(); 
     this.MapInfoInstance = map; 

     //What directory to look for! 
     String strPath = AssemblyDirectory; 
     using (var Catalog = new AggregateCatalog()) 
     { 
      DirectoryCatalog directorywatcher = new DirectoryCatalog(strPath, "*.dll"); 
      Catalog.Catalogs.Add(directorywatcher); 
      CompositionBatch batch = new CompositionBatch(); 
      batch.AddPart(this); 
      CompositionContainer container = new CompositionContainer(Catalog); 
      //get all the exports and load them into the appropriate list tagged with the importmany 
      container.Compose(batch); 

      foreach (var part in Catalog.Parts) 
      { 
       container.SatisfyImportsOnce(part); 
      } 
     } 

     Module.Run(); 
    } 
0

귀하의 주요 방법은 아마 다음과 같아야합니다

static void Main(string[] args) 
{ 
    var exeCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); 
    var dircatalog = new DirectoryCatalog("."); 
    var aggregateCatalog = new AggregateCatalog(exeCatalog, dirCatalog); 
    var container = new CompositionContainer(aggregateCatalog); 
    var program = container.GetExportedValue<Program>(); 

    program.Run(); 
} 

이 모듈에서 가져올 수있는 부분으로 사용할 수있는 ThirdPartyApp 클래스의 인스턴스를 만들려면, 당신은 두 가지 옵션이 있습니다.

public class ThirdPartyAppExporter 
{ 
    private readonly ThirdPartyApp thirdPartyApp = new ThirdPartyApp(); 

    [Export] 
    public ThirdPartyApp ThirdPartyApp { get { return thirdPartyApp; } } 
} 
:이 같은 클래스를함으로써 속성을 통해 내보낼 수 있습니다, 또는

container.ComposeExportedValue<ThirdPartyApp>(new ThirdPartyApp()); 

: 첫 번째는 명시 적으로이 같은 ComposeExportedValue 확장 메서드로 컨테이너에 같은 인스턴스를 추가하는 것입니다

관련 문제