MEF出现问题-ExportFactory< T> -调用Dispose方法 [英] Problem with MEF - ExportFactory<T> - call Dispose method

查看:119
本文介绍了MEF出现问题-ExportFactory< T> -调用Dispose方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对通过ExportFactory创建的对象调用dispose方法?

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

工厂在这里:

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;
    }
}

对象的创建:

var chatScreen = ViewModelControler.CreatChatViewModel();

我想打电话给chatScreen.Dispose().

ChatViewModel调用看起来像这样:

ChatViewModel call look like this:

[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IChatViewModel
    {}

推荐答案

您应该在对CreateExport()的调用返回的ExportLifetimeContext上调用dispose,而不是对导出的值本身进行调用.这样不仅可以处理ViewModelController,还可以处理为满足其导入而创建的所有NonShared一次性部件.

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.

这篇关于MEF出现问题-ExportFactory&lt; T&gt; -调用Dispose方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆