简体   繁体   中英

MEF - imported object is null in the constructor

I'm using MEF in order to inject UnityContainer into the plugins in my application.

each one of the Plugins hold panels.

I want to transfer the Unity container into the panels also by MEF.

I need to use the Unity in the constructor of the panels.

The problem is that the Unity is not getting initialized.

So, I use the CompositionContainer.ComposeParts(panel) in the Plugin after I created the panel. The problem is that I want to use the Unity in the constructor of the panels.

I don't want to send the the UnityContainer or the CompositionContainer as a parameter in the panel's constructor.

thanks

you dont have posted some code so i just assume:

if you do it that way:

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

if you do it this way

public class Bar
{
   [Import]
   private IMyImportedService service;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM