跟踪 MEF 容器创建的实例
我正在使用 MEF 创建同一导出的多个实例。
我想跟踪我创建的实例,并且正在查询容器或使用重新组合的集合,但我从来没有得到我的实例......
这是代码:
interface IFoo{};
[Export(typeof(IFoo)),PartCreationPolicy(CreationPolicy.NonShared)]
class Foo{};
class FooTracker{
CompositionContainer _container;
int HowManyValues()
{
// this always returns 1 and invokes a constructor
return _container.GetExportedValue<IFoo>().Count();
}
int HowManyExports(){
// idem
return _container.GetExports<IFoo>().Count();
}
// idem
[ImportMany(AllowRecomposition=true,AllowRecomposition=true)]
protected IEnumerable<IFoo> Foos { get; set; }
}
我想要得到的是已经存在的实例,如果没有则不创建新实例。
谢谢, 弗洛里安
I'm using MEF to create several instances of the same export.
I'd like to keep track of the instances I have created, and am either querying the container or using a recomposited collection, but I never get my instances...
Here's the code:
interface IFoo{};
[Export(typeof(IFoo)),PartCreationPolicy(CreationPolicy.NonShared)]
class Foo{};
class FooTracker{
CompositionContainer _container;
int HowManyValues()
{
// this always returns 1 and invokes a constructor
return _container.GetExportedValue<IFoo>().Count();
}
int HowManyExports(){
// idem
return _container.GetExports<IFoo>().Count();
}
// idem
[ImportMany(AllowRecomposition=true,AllowRecomposition=true)]
protected IEnumerable<IFoo> Foos { get; set; }
}
What I would like is to get is the already existing instances, not create a new one if there aren't any.
Thanks,
Florian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MEF 不提供此功能。您可以通过让 IFoo 的每个实现导入 IFooTracker 并调用 IFooTracker 中的方法来注册 IFoo 来完成此操作。
MEF doesn't provide this functionality. You could do it by having each implementation of IFoo import an IFooTracker and call a method in IFooTracker to register the IFoo.