[英]How to export the type when interfaces are derived from base interface in MEF
Below is my scenario. 以下是我的情况。 I am trying to associate the types to the interfaces by using the Export functionality during the initial bootstrap. 我试图在初始引导过程中使用“导出”功能将类型与接口相关联。 However, MEF complains on ImportCardinalityMismatchException.I am fairly new to MEF and I couldn't figure out what's wrong here? 但是,MEF抱怨ImportCardinalityMismatchException.MEF我还很陌生,我不知道这里出了什么问题? The easiest fix is to remove the inheritance. 最简单的解决方法是删除继承。 However, I would like to avoid it. 但是,我想避免这种情况。
public interface IColourService
{
Color GetColourByCountry(string countryName);
}
public interface IKnownColourService:IColourService
{
bool IsKnownCountry(string countryName);
}
public interface IUnKnownColourService:IColourService
{
bool IsUnKnownCountry(string countryName);
}
[Export(typeof(IColourService))]
public class ColourService:IColourService
{
//implementation
}
[Export(typeof(IKnownColourService))]
public class KnownColourService:IKnownColourService
{
//implementation
}
[Export(typeof(IUnKnownColourService))]
public class UnknownColourService:IUnKnownColourService
{
//implementation
}
Can you not use IColourService
as the Type in the Export attribute for all the classes? 您是否不能将IColourService
用作所有类的Export属性中的Type?
[Export(typeof(IColourService))]
Then, you can access them with the following property declaration: 然后,您可以使用以下属性声明访问它们:
[ImportMany]
public IEnumerable<IColourService> ColourServices { get; set; }
And add a helper method to get specific types: 并添加一个辅助方法来获取特定类型:
public IEnumerable<T> GetServices<T>()
{
return ColourServices.OfType<T>().ToList();
}
Hope this helps... 希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.