如何从MEF容器释放一个共享实例 [英] How to release a shared instance from a MEF container

查看:516
本文介绍了如何从MEF容器释放一个共享实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图释放一个共享实例或单值。有谁知道如何做到这一点?我必须刷新目录?我正在学习MEF,请帮助。

I am trying to release a shared instance or singleton value. Does anyone know how to do this? Do I have to refresh the catalog? I'm learning MEF so please help.

类的实例

[Export]
public class Foo
{
  public RandomProperty {get;set;}

  [ImportConstructor]
  public Foo() {}
}

您可以像这样创造的:

var fooSingleton = ServiceLocator.GetInstance(typeof(Foo));



所有罚款和好,但我非常希望做这样的事情。

All fine and good, but ideally I would like to do something like this

Container.Replace(oldFoo, newFoo);



所以,当我再次把它叫做

So when I call it again

var fooSingleton = ServiceLocator.GetInstance(typeof(Foo));



fooSingleton将具有新的价值。

fooSingleton will have the new value.

我想答案可能依赖于实际清理出目录,然后刷新了它 - 但这似乎矫枉过正这样一个简单的事情。

I think the answer probably relies in actually clearing out the catalog and then refreshing it - but this seem overkill for such a simple thing.

推荐答案

在默认情况下在MEF,当你创建一个出口,则是共享的。在许多其他容器中,这被称为单例的生活方式。这意味着释放的出口会做什么,因为容器需要挂到出口的其他潜在的消费者

By default in MEF, when you create an Export, is is shared. In many other containers, this is referred to as the Singleton lifestyle. This means that releasing the export will do nothing, since the container needs to hang on to the export for other potential consumers.

您真的有在你面前的两个选项:

You really have 2 options in front of you:


  1. 废弃容器,假设你用它做。当应用程序被关闭,例如这是合适的。

  2. 更改部分是暂时的对象,即每次申请一个从容器中的时间创建一个新的组成部分。在MEF做到这一点,添加一个PartCreationPolicy属性导出并指定要非共享。这应该是这样的: [PartCreationPolicy(CreationPolicy.NonShared)] 。这将导致你的部分被称为的Dispose 方法时,名为 container.ReleaseExport(myExport),其中 myExport 是围绕保持释放目的的出口(不导出值)。

  1. Dispose the container, assuming that you are done with it. This is appropriate when the application is shutting down for example.
  2. Change your parts to be transient objects, that is a new part is created each time you request one from the container. To do this in MEF, add a PartCreationPolicy attribute to the Export and specify it is to be non-shared. This would look like this: [PartCreationPolicy (CreationPolicy.NonShared)]. This will cause the Dispose method to be called on your parts when container.ReleaseExport(myExport) is called where myExport is an export (not an exported value) that is kept around to releasing purposes.

下面是一个例子:

var catalog = new AggregateCatalog(// code elided);
var container = new CompositionContainer(catalog);

Lazy<IMyExportInterface> myExport = container.GetExport<IMyExportInterface>();
// later on...
container.ReleaseExport(myExport)



这说明你需要,你可以访问MEF容器要做到这一点,并在你保持出口的引用。

This shows that you need to do this where you have access to the MEF container, and where you have kept a reference to the export.

注意,但是。作为反射用于创建每个新对象改变到暂时对象,而不是单身将影响容器的性能。

Caution, however. Changing to transient objects instead of singletons will affect the performance of the container as reflection is used to create each new object.

这篇关于如何从MEF容器释放一个共享实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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