【发布时间】:2017-01-06 11:57:51
【问题描述】:
前言:我知道有很多关于协变和逆变的问题和答案,但我仍然感到困惑,不知道要实施什么解决方案。
我有两个接口,它们的实现打算成对一起使用。一个提供有关销售项目的信息,另一个提供销售项目的语言相关信息。
我无法控制这些界面:
public interface IItem
{
decimal Price { get; set; }
}
public interface IItemTranslation
{
string DisplayName { get; set; }
}
对于有形的GoodsItem 和无形的ServiceItem,我也有这两个接口的两个实现。同样,我无法控制这些界面:
public class GoodsItem : IItem
{
public decimal Price { get; set; } //implementation
public float ShippingWeightKilograms { get; set; } //Property specific to a GoodsItem
}
public class GoodsTranslation : IItemTranslation
{
public string DisplayName { get; set; } //implementation
public Uri ImageUri { get; set; } //Property specific to a GoodsTranslation
}
public class ServiceItem : IItem
{
public decimal Price { get; set; } //implementation
public int ServiceProviderId { get; set; } // Property specific to a ServiceItem
}
public class ServiceTranslation : IItemTranslation
{
public string DisplayName { get; set; } //implementation
public string ProviderDescription { get; set; } // Property specific to a ServiceTranslation
}
正如我所说,这些是我无法控制的类。我想创建这些配对的通用列表 (List<Tuple<IItem, IItemTranslation>>),但我不能:
public class StockDisplayList
{
public List<Tuple<IItem, IItemTranslation>> Items { get; set; }
public void AddSomeStockItems()
{
Items = new List<Tuple<IItem, IItemTranslation>>();
var canOfBeans = new Tuple<GoodsItem, GoodsTranslation>(new GoodsItem(), new GoodsTranslation());
var massage = new Tuple<ServiceItem, ServiceTranslation>(new ServiceItem(), new ServiceTranslation());
Items.Add(canOfBeans); //illegal: cannot convert from 'Tuple<GoodsItem, GoodsTranslation>' to 'Tuple<IItem, IItemTranslation>'
Items.Add(massage); //illegal: cannot convert from 'Tuple<ServiceItem, ServiceTranslation>' to 'Tuple<IItem, IItemTranslation>' }
}
问题:在不更改我的 IItem 和 ITranslation 类或其派生类型的情况下,能够传递这些配对的通用列表而不来回转换它们的最简洁方法是什么接口和它们的类型之间?
警告。我试图简化问题,但我实际上并没有使用元组。实际上,我正在使用这样的类:
public class ItemAndTranslationPair<TItem, TItemTranslation> where TItem : class, IItem where TItemTranslation : class, IItemTranslation
{
TItem Item;
TTranslation Translation;
}
我的服务返回强类型列表,例如 List<ItemAndTranslationPair<GoodsItem, GoodsTranslation>>,因此当我将项目添加到“通用”列表时,它看起来像:
var differentBrandsOfBeans = SomeService.GetCansOfBeans();
//above variable is of type IEnumerable<ItemAndTranslationPair<GoodsItem, GoodsTranslation>>
var items = new List<ItemAndTranslationPair<IItem, IItemTranslation>>();
items.AddRange(differentBrandsOfBeans);
【问题讨论】:
标签: c# generics covariance contravariance invariance