Non-attributed types & MEF

Mar 28, 2010 at 8:13 PM

What is the simplest way to use existing types as parts with MEF? Say I'm using a library that defines some interfaces and implementations of those interfaces. I want to use those types as parts in my MEF-based application, but I don't own the source code and therefore cannot add Export attributes. For example, I want use ArrayList as an export of IList (goofy example, but you get the idea). I also want CompositionContainer to manage the lifetime of this object. When I compose a part that imports an IList, I'd like MEF to inject the ArrayList. I understand this is basically trying to use MEF without the default attributed programming model, but I'm not sure what all would be involved in this fairly simple scenario. Do I need to create a custom ComposablePart, ComposablePartDefinition, ExportDefinition, something else entirely?

Thanks!
Ryan

Mar 28, 2010 at 8:34 PM

Hi Ryan

The easiest way to reuse existing types is to use a property export. This post explains how they work: http://codebetter.com/blogs/glenn.block/archive/2010/01/27/how-do-i-expose-configuration-information-through-mef.aspx

Basically a property export is a part with a custom factory of sorts where you can create your own thing and make it appear to MEF as an export.

Cheers