MEF怎么删除部件,有一段代码始终运行不正确?
http://mef.codeplex.com/wikipage?title=Parts%20Lifetime&referringTitle=Guide
从上面拷贝的代码运行始终不正确,怎么才能正确的删除部件?
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
class Program
{
static void Main(string[] args)
{
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
var container = new CompositionContainer(catalog);
var root = new Root();
// add external part
container.ComposeParts(root);
// ... use the composed root instance
// removes external part
var batch = new CompositionBatch();
//这里始终报错???
batch.RemovePart(root);
container.Compose(batch);
}
}
public class Root
{
[Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
public NonSharedDependency Dep { get; set; }
}
[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class NonSharedDependency : IDisposable
{
public NonSharedDependency()
{
}
public void Dispose()
{
Console.WriteLine("Disposed");
}
}
[最优解释]
试试下面这段代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
var container = new CompositionContainer(catalog);
var root = new Root();
container.ComposeParts(root);
var batch = new CompositionBatch();
var part = batch.AddExportedValue<NonSharedDependency>(new NonSharedDependency());
container.Compose(batch);
Console.ReadLine();
}
}
public class Root
{
[Import(RequiredCreationPolicy = CreationPolicy.NonShared, AllowRecomposition=true)]
public NonSharedDependency Dep { get; set; }
}
[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class NonSharedDependency : IDisposable
{
public NonSharedDependency()
{
Console.WriteLine("Created an instance of NonSharedDependency");
}
public void Dispose()
{
Console.WriteLine("Disposed");
}
}
}
注意:我没有用RemovePart,因为我也没搞清楚它原帖到底想写个什么例子。但是如果你是想知道MEF对对象生命周期的管理。我可以给你简单解释一下。根据官方文档:
We believe that .Net Garbage Collector is the best thing to rely on for proper clean up. However, we also need to provider a container that has a deterministic behavior. Thus, the container will not hold references to parts it creates unless one of the following is true:
The part is marked as Shared
The part implements IDisposable
One or more imports is configured to allow recomposition
我们可以知道,如果任何Part被有Shared属性,或它已实现IDisposable接口,或有一个或更多Imports的AllowRecomposition为True,那么对于这些Parts,MEF会保留对它们的引用。
如果你运行我的代码,你会发现Dispose被调用了一次。这是因为
var batch = new CompositionBatch();
var part = batch.AddExportedValue<NonSharedDependency>(new NonSharedDependency());
container.Compose(batch);
这三行代码强迫container去重新构建它自己,所以原先的NonSharedDependency export被替换为batch中的了,而在替换过程中,首先新的NonSharedDpependency被构建,之后原先的NonSharedDependency对象会被显式释放并被新的替换掉,这个替换的动作会主动调用其Dispose方法。
[其他解释]
貌似没多少人对MEF感兴趣。我随便说点吧。刚才看了一下链接,这部分代码确实有编译错误,下面有人提到了这个问题:The "AddPart/RemovePart" example does not compile (even with the missin 'var' correction).意思是说这段代码无法编译。
你其实不用纠结这个代码,它只是一个示例而已。有可能这段代码已经过时了,跟最新的MEF的API接不上。RemovePart这个函数只接受ComposablePart。如果你用batch.AddExportedValue,你可以得到一个ComposablePart返回值——只有这种类型的实例才可以被RemovePart接受。至于这段代码,就是想告诉你MEF可以RemovePart而已。但是需要注意,如果你没有把AllowRecomposition设置成True,那么你是不可以动态RemovePart的,实时运行时会报错。
[其他解释]
你好,我试了一下,可是还是没有删除:
// removes external part
var batch = new CompositionBatch();
var part = batch.AddExportedValue(root);
//这里始终报错???
batch.RemovePart(part);
container.Compose(batch);
[其他解释]
我不太清楚你的目的。你是想删除什么?
[其他解释]
public void Dispose()
{
Console.WriteLine("Disposed");
}
例子的意思就是Dispose没有调用啊?删除部件后就应该调用啊?
[其他解释]
多谢,已经有问题还需请教一下你。