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

Non-generic GetExportedValue method

Aug 2, 2009 at 8:46 PM

I wonder why I couldn’t query the container for an export using a non-generic GetExportedValue method instead of using the generic method: object value = GetExportedValue(type);

I think that it would be great to expose such method especially for getting exports defined by configuration files, DB, reflection, XAML, etc.

To overcome this I’ve added an overload method to the container, but this solution is temporary and will only last until the release of MEF.

Aug 27, 2009 at 7:30 PM

Instead of changing the MEF library have you considered building a invocation of the generic method using reflection.  Here is an example.

<font size="2">



// Get the method <font size="2">



Type containerType = Program.CompositionContainer.GetType();

MethodInfo methodInfo = containerType.GetMethod("GetExportedValueOrDefault", new Type[] { });

// Make the generic method with the type <font size="2">



Type[] argTypes = { serviceType };

MethodInfo genericMethodInfo = methodInfo.MakeGenericMethod(argTypes);

service = genericMethodInfo.Invoke(Program.CompositionContainer, new object[] { });

Aug 27, 2009 at 8:44 PM

Nice solution but you could save the generic stuff by calling the private method directly, and IMHO it will be ridiculous to use reflection just because the necessary method exist but private.

Anyway, you may find the solution here: