1

If possible call dispose method on object which is created with ExportFactory?

Factory is here:

public  interface IViewModelsControler
{
    IChatViewModel CreatChatViewModel();
}

[Export(typeof(IViewModelsControler))]
public class ViewModelsControler:IViewModelsControler
{

    [Import]
    public ExportFactory<IChatViewModel> ChatViewFactory { get; set; }

    public IChatViewModel CreatChatViewModel()
    {
        return ChatViewFactory.CreateExport().Value;
    }
}

Creation of object:

var chatScreen = ViewModelControler.CreatChatViewModel();

I would like call chatScreen.Dispose().

ChatViewModel call look like this:

[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IChatViewModel
    {}
jcolebrand
  • 15,889
  • 12
  • 75
  • 121

2 Answers2

2

You should call dispose on the ExportLifetimeContext returned by the call to CreateExport(), not on the exported value itself. This will dispose not just the ViewModelController, but any NonShared disposable parts that were created to satisfy its imports.

Daniel Plaisted
  • 16,674
  • 4
  • 44
  • 56
0

Your contract for chatScreen needs to expose the Dispose() method.

public interface IViewModelsControler
{
    IChatViewModel CreatChatViewModel();
    void Dispose();    // add to expose your dispose method
}

Here is another answer regarding garbage collection if that is what you are after.

Community
  • 1
  • 1
IAbstract
  • 19,551
  • 15
  • 98
  • 146
  • Is this down-voted because I'm incorrect in answering the question, " *how would* I ... call `chatScreen.Dispose()`?", or because this method should not be done? It would be helpful to know why my answer is wrong. – IAbstract Jan 05 '11 at 01:12