2012-01-02 99 views
0

我使用MEF爲了將UnityContainer插入到我的應用程序的插件中。MEF - 導入的對象在構造函數中爲null

每個插件都包含面板。

我想通過MEF將Unity容器轉移到面板中。

我需要在面板的構造函數中使用Unity。

問題是Unity沒有被初始化。

因此,在創建面板後,我在插件中使用了CompositionContainer.ComposeParts(面板)。 的問題是我想在面板的構造函數中使用Unity。

我不想在面板的構造函數中將UnityContainer或CompositionContainer作爲參數發送。

感謝

+3

你應該發佈一些代碼來初始化MEF(創建目錄)以及你如何使用Imports/Exports屬性。 – 2012-01-02 16:30:45

回答

3

你不已經發布了一些代碼,所以我只是假設:

,如果你這樣做的:如果你這樣做這樣

public class Bar 
{ 
    [ImportingConstructor] 
    public Bar(IMyImportedService service) 
    { 
     //service should not be null 
    } 
} 

public class Bar 
{ 
    [Import] 
    private IMyImportedService service; 

    public Bar() 
    { 
     //service should be null, because you have to implement IPartImportsSatisfiedNotificationand use OnImportsSatisfied 
    } 
}