This project has moved. For the latest updates, please go here.

Force control to be created.

Feb 9, 2010 at 6:01 PM

I have such a class

  [Export("InventoryTypeInfoControl", typeof(IDialogServiceChild))]
  public partial class InventoryTypeInfoControl : BaseControl, IDialogServiceChild, IInventoryTypeInfoView
    private IUICore _uiCore;

    [Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
    private InventoryTypeInfoPresenter _presenter;

    public InventoryTypeInfoControl()

    public override void OnImportsSatisfied()

 now when I need it I use such a construction



    public T GetRequiredService<T>(string serviceName)
      return Program.Container.GetExportedValue<T>(serviceName);      

InventoryTypeInfoControl child = (InventoryTypeInfoControl)_uiCore.GetRequiredService<IDialogServiceChild>("InventoryTypeInfoControl");

it works ok, it gets created. The problem is, that if I call second time the GetRequiredService, the MEF won't create this class again, but it will use the previous copy, thus the method OnImportsSatisfied won't be called and I won't be able to initialize the _presenter as needed.
 How can I say to MEF to always recreate a needed class?




Feb 9, 2010 at 9:33 PM

Decorate your class with


Feb 10, 2010 at 5:21 AM

Is there a way to force partcreationpolicy on GetExportedValue though?

Feb 10, 2010 at 12:29 PM

No, you either have to use an import with RequiredCreationPolicy, or decorate your class with a partcreationpolicy.

I experimented a bit with trying to pull a PartCreator<T> directly from the container but currently there doesn't seem to be a way to do that. OTOH a normal import of PartCreator<T> may be what you are looking for.