MEF 问题 - ExportFactory- 调用Dispose方法
如果可能的话,对使用 ExportFactory 创建的对象调用 dispose 方法?
工厂在这里:
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 调用如下所示:
[Export(typeof(IChatViewModel))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ChatViewModel : Screen, IChatViewModel
{}
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
{}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该对调用 CreateExport() 返回的 ExportLifetimeContext 调用 dispose,而不是对导出的值本身调用 dispose。这不仅会处理 ViewModelController,还会处理任何为满足其导入而创建的非共享一次性部件。
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.
您的 chatScreen 合约需要公开 Dispose() 方法。
这是关于垃圾收集的另一个答案(如果这就是您所追求的)。
Your contract for chatScreen needs to expose the Dispose() method.
Here is another answer regarding garbage collection if that is what you are after.