Force control to be created.

Feb 9, 2010 at 7:01 PM

Hi
I have such a class

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

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

    public InventoryTypeInfoControl()
    {
      InitializeComponent();
    }

    public override void OnImportsSatisfied()
    {
      InitTabs();
  
      _presenter.Init(this);
    }

 now when I need it I use such a construction

 

//UICore...

    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?

Thx

 

 

Feb 9, 2010 at 10:33 PM

Decorate your class with

[PartCreationPolicy(CreationPolicy.NonShared)]

Feb 10, 2010 at 6:21 AM

Is there a way to force partcreationpolicy on GetExportedValue though?

Feb 10, 2010 at 1: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.