我在玩LinqToSql和Im,试图为我的一个类实现存储库模式。当我试图将一个属性EntitySet映射到IList时,问题就出现了,我得到了一个错误
'TheCore.Models.User‘不实现接口成员'TheCore.Models.IUserRepository.Vehicles’。'TheCore.Models.User.Vehicles‘不能实现'TheCore.Models.IUserRepository.Vehicles’,因为它没有匹配的返回类型'System.Collections.Generic.IList‘
EntitySet似乎实现了IList,所以为什么我不能将IList属性映射到EntitySet属性?
EntitySet:
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Users_Vehicles", Storage="_Vehicles", ThisKey="Id", OtherKey="FkOwnerId")]
public EntitySet<Vehicle> Vehicles
{
get
{
return this._Vehicles;
}
set
{
this._Vehicles.Assign(value);
}
}存储库接口:
IList<Vehicle> Vehicles { get; set; }发布于 2014-05-10 14:05:11
接口实现的返回类型必须与接口中声明的返回类型匹配。这称为返回类型协方差,C#不支持它。
因此,即使List实现了IList,下面的代码也不能工作
public interface IFoo
{
IList<string> Foos {get; set;}
}
public class Foo : IFoo
{
public List<string> Foos {get; set;}
}看看这个问题:"Interface not implemented" when Returning Derived Type
发布于 2014-05-10 14:06:13
我可能不理解您的问题,但似乎您有一个与方法IList<Vehicle> Vehicles { get; set; }的接口,并试图通过提供一个实现public EntitySet<Vehicle> Vehicles来履行合同。这是不允许的--实现必须提供与接口相同的返回类型(在本例中为IList<Vehicle>)。如果可以,将存储库实现更改为包装EntitySet,然后将该方法与所需接口匹配:
public class Vehicle
{
}
public interface IRepository
{
IList<Vehicle> Vehicles { get; set; }
}
public class Repository : IRepository
{
private EntitySet<Vehicle> _Vehicles;
public IList<Vehicle> Vehicles
{
get
{
return this._Vehicles;
}
set
{
this._Vehicles.Assign(value);
}
}
}https://stackoverflow.com/questions/23581891
复制相似问题