tags:

views:

37

answers:

1

I am trying to use MEF to Export the following:

[Export(typeof(IRepository<>))]
public class Repository<T> : IRepository<T>
    where T : class
{

With an import of

[Import(typeof(IRepository<>))]
private IRepository<Contact> repository;

But I keep getting an error message when composing MEF of:

=========================================

The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No valid exports were found that match the constraint '((exportDefinition.ContractName = "Interfaces.IRepository()") && (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") && "Interfaces.IRepository()".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.

Resulting in: Cannot set import 'SoCLINQ2SQL.RepositoryTest.repository (ContractName="Interfaces.IRepository()")' on part 'SoCLINQ2SQL.RepositoryTest'. Element: SoCLINQ2SQL.RepositoryTest.repository (ContractName="Interfaces.IRepository()") --> SoCLINQ2SQL.RepositoryTest

+1  A: 

To the best of my knowledge, and according to Glenn Block's post on the subject, MEF does not support open generic types "out of the box".

Apparently there is support for it in the MEF contrib project.

I believe in that case you would be able to leave your export as an open generic type but on the import side you would need to change your import to look like:

[Import(typeof(IRepository<Contact>))]
private IRepository<Contact> repository;
Josh Einstein
Many thanks :-)
Coppermill