I am trying to leverage the benefits of Generics with and MEF solution, however I am struggling to get things to register. (Am using MEF 2, .Net 4.51).
Here is a simplified version of my classes and interfaces that I would like to work:
public interface IAnimal
{
string Speak();
}
public interface IAnimalCatcher<T> where T : IAnimal
{
string WhatICatch();
T AnimalCaught { get; set; }
}
[Export(typeof(IAnimal))]
public class Dog : IAnimal
{
public string Speak()
{
return "Woof";
}
}
//[Export(typeof(IAnimalCatcher<IAnimal>))]
//[Export(typeof(IAnimalCatcher<Dog>))]
public class DogCatcher: IAnimalCatcher<Dog>
{
public string WhatICatch()
{
return "Dogs";
}
public Dog AnimalCaught { get; set; }
}
and the code which does the composition:
public class Program
{
private CompositionContainer _container;
[Import(typeof(IAnimal))]
public IAnimal TheAnimal;
[Import(typeof(IAnimalCatcher<>))]
public IAnimalCatcher<IAnimal> TheAnimalCatcher;
public Program()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
_container = new CompositionContainer(catalog);
this._container.ComposeParts(this);
}
}
I cannot get the Dog Class to export and import correctly. I have tried several combination without success (Commented above the classes).
The error is as follows:
System.ComponentModel.Composition.ChangeRejectedException was unhandled Message=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 exports were found that match the constraint: ContractName MEFGeneric.IAnimalCatcher(MEFGeneric.IAnimal) RequiredTypeIdentity MEFGeneric.IAnimalCatcher(MEFGeneric.IAnimal)
Resulting in: Cannot set import 'MEFGeneric.Program.TheAnimalCatcher (ContractName="MEFGeneric.IAnimalCatcher(MEFGeneric.IAnimal)")' on part 'MEFGeneric.Program'. Element: MEFGeneric.Program.TheAnimalCatcher (ContractName="MEFGeneric.IAnimalCatcher(MEFGeneric.IAnimal)") --> MEFGeneric.Program
Note that IAnimalCatcher<IAnimal>
will register, but that doesn't let me limit the dog catcher to dogs.
Is there a way to address this through importing, or is my design flawed, and should I be implementing DogCatcher differently?