This project has moved and is read-only. For the latest updates, please go here.

Hook to post-process object after creation

Sep 1, 2011 at 9:23 PM

I have question on MEF.

Can I in some way hook into MEF and post-process all object created by MEF?

I want do some reflection (read interfaces or annotations) and modify created object in some way.

This hooking mechanism must include singleton, shared instances and ExportFactory.


Sep 7, 2011 at 12:11 AM

MEF does not support customizable instantiation or initialization process - the only way to achieve this is at the part level where you can do the post-initialization in the constructor itself or in the OnImportsSatisfied() (assuming the part implements IPartImportsSatisfiedNotification interface)

Sep 7, 2011 at 12:32 AM

MEF doesn't support it in the box, but there is an extension in MEF Contrib called the InterceptingCatalog that allows you to hook in for example to wire up to an AOP mechanism like Castle Dynamic Proxy. You can also use it to just plug in your own custom code.

You can read more on the catalog here:

Sep 7, 2011 at 7:48 AM

I look at it.

And yes, I need it for AOP.

Pretty common in middle to large projects.
I'm surprised that MEF itself can't do it.


Sep 7, 2011 at 8:09 AM

We actually did talk about the scenario in V1.  The original InterceptingCatalog was something I contributed (though it has changed significantly). I built it to validate that we could at least support the scenarios, which you can through a custom MEF programming model (i.e. custom catalog etc).

The reason why we didn't do it ourselves was it was a matter of resource and priority. Supporting AOP was considered low on the scale compared to other key features necessary to support our third party extensibility story. We made a lot of tradeoffs during development in order to have a great story for our core scenario, i..e third party extensibility. AOP is nice to have in particular for the non-3rd party / internal app decoupling scenarios.