2012-01-02 33 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 
    } 
} 
相关问题