2013-08-13 1 views
4

속성 가져 오기가 언제 만족됩니까? 나는 그들이 생성자 전에 만족 될 것이라고 생각했다. 생성자가 실행되기 전에 프로퍼티가 초기화 되었기 때문에, 다음 예제는 ImportedClass이 생성자에서 null이 될 것을 보여준다.언제 부동산 수입이 만족합니까?

저는 이것을 ImportingConstuctor를 사용하여 해결할 수 있음을 알고 있습니다. 이는 속성 가져 오기가 만족되는 시점을 이해하기위한 것입니다.

public MyClass 
{ 
    [Import] 
    public ImportedClass ImportedClass {get;set;} 

    public MyClass() 
    { 
     //Imported Class is null at this point, so nothing can be done with it here. 
    } 
} 

답변

6

개체는 생성자가 호출되기 전에 조작 할 수 없습니다. MEF는 MEF 당신의 수입을 설정하는 데 걸리는 작업에 대한 IPartImportsSatisfiedNotification

public MyClass : IPartImportsSatisfiedNotification 
{ 
    [Import] 
    public ImportedClass ImportedClass {get;set;} 

    public MyClass() 
    { 
     //Imported Class is null at this point, so nothing can be done with it here. 
    } 

    public void OnImportsSatisfied() 
    { 
    //ImportedClass is set at this point. 
    } 
} 

라는 인터페이스,하지만 당신의 문제에 대한 솔루션을 제공합니다; 먼저 생성자를 호출하고 속성을 설정 한 다음 알림 메서드를 호출합니다.