我有以下的抽象类和接口:替换基类的具体实现C#
public interface ITypedEntity{
TypedEntity Type { get; set; }
}
public abstract class TypedEntity:INamedEntity{
public abstract int Id{get;set;}
public abstract string Name { get; set; }
}
但是当我试图创建一个实现ITypedEntity并具有TypedEntity的concrecte实现一类我得到以下错误
Magazine does not implement interface member ITypedEntity.Type. Magazine.Type cannot implement 'ITypedEntity.Type' because it does not have the matching return type of 'TypedEntity'
我concrecte实现的代码如下
public class Magazine:INamedEntity,ITypedEntity
{
public int Id {get;set;}
[MaxLength(250)]
public string Name {get;set;}
public MagazineType Type { get; set; }
}
public class MagazineType : TypedEntity
{
override public int Id { get; set; }
override public string Name { get; set; }
}
我想我明白发生了什么,但我不知道为什么,因为MagazineType是一个TypedEntity。
谢谢。
更新1 我不得不提一下,我想利用这个班,EF CodeFirst,我想有一个不同的表中的每个实现ITypedEntity类的。
MagazineType是TypedEntity但TypedEntity我没有MagazineType。也许你可以像'ITypedEntity Type {get;组; }' –
但是,存储在“ITypedEntity”类型的变量中的引用“杂志”的代码可能会尝试将*不是'MagazineType'的内容存储到'Type'属性中。 –
将'Magazine.Type'的返回类型更改为'EntityType'。它仍然可以是'MagazineType'的一个实例,但签名必须匹配才能满足接口实现的要求。 – Jodrell