【发布时间】:2014-03-17 17:19:03
【问题描述】:
在我的应用中,我使用不同的网络 API 来获取汽车信息。对于我已经实现 ICarService 的服务。由于所有这些 Api 返回的汽车数据集略有不同,因此我实现了 ICar 接口,因此每个服务都可以返回自己的汽车类型,但在我的应用程序中,我可以使用“通用”ICar。
这是我的实现:
// Car Model
public interface ICar
{
string Color { get; }
}
public class CarApiA : ICar
{
public int car_color { get; set; }
public Color
{
get { return this.car_color; }
}
}
// Service
public interface ICarService
{
Task<List<ICar>> GetCarsAsync(string search);
}
public class CarApiAService : ICarService
{
public async Task<List<CarApiA>> GetCarsAsync(string search)
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(url);
string content = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<List<CarApiA>>(content);
}
}
现在我收到错误消息“'CarApiAService' does not implement interface member 'ICarService.GetCarsAsync(string)'. 'CarApiAService.GetCarsAsync(string)' cannot implement 'ICarService.GetCarsAsync(string)' 因为它没有'System.Threading.Tasks.Task>' 的匹配返回类型。”
如何使用接口返回接口?如果这是我的实现想法完全错误,请指导我正确的方向。
【问题讨论】:
-
您的
Task<List<CarApiA>>与ICarService接口中的合约不匹配。这就是问题所在。只需将其更改为Task<list<ICar>>。这将起作用,因为CarApiA是ICar。 -
您是否尝试过在 CarApiAService 中将 GetCarsAsync 的返回类型更改为 Task
- > ?
-
当您指定一个泛型 List 时,它必须只包含该类型的对象 - 不能使用子类。
-
@PugFugly - 您当然可以将子类型的实例放入通用列表中,但您不能将
List<Subclass>分配给List<Base>。 -
是的,对不起,我的意思是分配整个列表而不是添加单个元素。我的坏
标签: c# windows-phone-8 interface