2012-06-25 6 views
1

미리 도움을 주셔서 감사합니다. 나는 다음과 같은 수출 부분이 있습니다MEF Composition .NET 4.0

[Export (typeof(INewComponent))] // orignally tried just [Export} here and importing NewComponent below 
public class NewComponent : INewComponent 
{ 
    // does stuff including an import 
} 

콘솔 테스트 프로그램이 수입을 위 :

: 조성은 이러한 CompositionExceptions 실패

public class Program 
{  

    [Import] // have tried variations on importing "NewComponent NewComponent" etc 
    public INewComponent NewComponent 
    { 
     get; 
     set; 
    } 

    public static void Main(string[] args) 
    { 
     var p = new Program(); 
     var catalog = new AssemblyCatalog(typeof(Program).Assembly); 
     var container = new CompositionContainer(catalog); 
     container.ComposeParts(p); 
} 

는 (나는 :) 유죄를 보호하기 위해 네임 스페이스를 제거)

1) '((exportDefinition.ContractName == "INewComponent") 및(exportDefinition)과 일치하는 유효한 내보내기가 없습니다. .Metadata.ContainsKey ("ExportTypeIdentity") AndAlso "INewComponent".Equals (exportDefinition.Metadata.get_Item ("ExportTypeIdentity")))) ', 잘못된 내보내기가 거부되었을 수 있습니다.

이 같은 메인 프로그램에서 구성을 할 경우 구성이 성공적으로 작동합니다

public class Program 
{  

    public static void Main(string[] args) 
    { 
     INewComponent newComponent = new NewComponent(); 

     var catalog = new AssemblyCatalog(typeof(Program).Assembly); 
     var container = new CompositionContainer(catalog); 
     container.ComposeParts(newComponent); 
    } 
} 

답변

3

가 내 보낸 부분은 Program 같은 어셈블리에 포함되어 감사합니다? 이 별도의 DLL에있는 경우,이 같은뿐만 아니라 카탈로그에 해당 어셈블리를 포함해야합니다

var aggregateCatalog = new AggregateCatalog(); 
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); 
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(NewComponent).Assembly)); 
var container = new CompositionContainer(aggregateCatalog); 
// etc... 

작동하지 않는 것 경우, 다음 Visual MEFx라는 좋은 오픈 소스 도구가 있다고 할 수 카탈로그 분석에 도움이됩니다.

// does stuff including an import 

그 도시되지 않은 수입에 문제가있는 경우, MEF는 불평 것입니다 : 당신이 쓴 당신의 NewComponent 클래스에서

Getting Started With Visual MEFx

+1

짐 감사합니다. 네, NewComponent가 DLL에 있었고 위의 트릭을했습니다. 다시 돌아가서 MEF에서 Block의 원본 MSDN 기사를 다시 읽어야합니다. h2에 대한 블로그의 Thx는 Visual MEFX와 함께합니다. 완전한. –

2

: 여기에 짧게 설정에 대한 기사입니다 실제로 더 깊은 원인 대신 Program.NewComponent 가져 오기. 이것을 "안정된 구성"이라고합니다. Stable composition can be useful이지만, it also complicates the debugging of a failed composition.

Diagnosing Composition Errors에 관한 MEF 설명서의 지침에 따라 실제 원인을 파악할 수 있습니다.

작은 프로그램에서 문제가되는 것을 찾을 때까지 container.GetExportedValue<ISomeExport>()에 몇 가지 내보내기를 시도 할 수 있습니다.

+0

감사합니다. 네, 저는 안정된 구성 항목에 익숙합니다 :}. 이 기사에서 SKU 구성에 대한 생각은 매우 흥미 롭습니다. –