[英]How to export the type when interfaces are derived from base interface in MEF
以下是我的情況。 我試圖在初始引導過程中使用“導出”功能將類型與接口相關聯。 但是,MEF抱怨ImportCardinalityMismatchException.MEF我還很陌生,我不知道這里出了什么問題? 最簡單的解決方法是刪除繼承。 但是,我想避免這種情況。
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
}
您是否不能將IColourService
用作所有類的Export屬性中的Type?
[Export(typeof(IColourService))]
然后,您可以使用以下屬性聲明訪問它們:
[ImportMany]
public IEnumerable<IColourService> ColourServices { get; set; }
並添加一個輔助方法來獲取特定類型:
public IEnumerable<T> GetServices<T>()
{
return ColourServices.OfType<T>().ToList();
}
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.