在MEF拦截依赖MEF

2023-09-03 08:02:38 作者:戒不掉の思念

是否有可能拦截依赖请求,MEF他们通过MEF得到处理过吗?

Is it possible to intercept dependency requests in MEF before they get handled by MEF?

这将是实现装饰和先进的生命周期管理是有用的。

This would be useful for implementing decorators and advanced lifetime management.

喜欢的东西...

catalogue.AddInterceptor<IExpensiveService>(b => ... return from pool ...);

甚至...

Or even...

catalogue.AddInterceptor<IExpensiveService>(b => new Decorator(b()));

(其中B是底层MEF分辨率FUNC键解决服务)

(where 'b' is the underlying MEF resolution func for resolving the service)

推荐答案

不开箱即用,但你可以编写自己的 ExportProvider 或 ComposablePartCatalog 实施做到这一点。

Not out of the box, but you can write your own ExportProvider or ComposablePartCatalog implementation to do this.

MefContrib 似乎已经实现了这样的事情,看看 InterceptingCatalog 。也看到这个(可能是过时的)博客帖子一下吧。

MefContrib appears to have implemented something like that, take a look at InterceptingCatalog. See also this (possibly outdated) blog post about it.