Extension methods and MEF

May 10, 2010 at 9:43 PM

Is it possible to use variables filled with MEF in an extension method? I am sing the .NET 4 API (no SatisfyImports).

The scenario...

I have an assembly that contains a set of interface contracts. I would like programmers to be able to report the contents of the graphs contained in the impelmentation in a standard manner. I would like to do this without making any demands of the implementors.

One way to do this is to provide extension methods in the same assembly as the contracts. You've got the contracts, you've got the extension methods.

So far, so good.

But, the reporting is in XML and I really, really, really want to do this in VB (OK, I'm doing this in VB and there is no way I'm going to change that to C# although most of the rest of this app is in C#).

So, I have a second assembly for the VB part. It basically has a ReportAsXmlString method taking an instance of the graph root contract interface. Cool.

But to allow the VB assembly to access the contract interfaces, I need a reference from the VB support assembly to the main contract assembly. That means I can't have a hard reference from the contract assembly to the VB assembly because of circular reference silliness. I need a MEF reference here, although the only reason is to snip the circular reference dependency - these two assemblies could otheriwse be intimate.

So, now I have an extension method that wants to call something in a class I need to access via MEF. I can think of a couple of hacks (and have used a few hacks before) but they require that I do something at either composition-time or in the calling code allowing this design detail to leak out.

I'm about to give up on this problem and require all implementors to report themselves, and to have references to the support assembly, but man is that ever an ugly solution. Does anyone have a better solution?