Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用具体的C实现替换基类#_C#_Asp.net_Entity Framework_Oop - Fatal编程技术网

C# 用具体的C实现替换基类#

C# 用具体的C实现替换基类#,c#,asp.net,entity-framework,oop,C#,Asp.net,Entity Framework,Oop,我有以下抽象类和接口: 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的类并具有具体的T

我有以下抽象类和接口:

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实现时,我得到了以下错误

 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'
下面是我的conrecte实现的代码

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是一种类型识别

谢谢

更新1
我必须提到的是,我想将这个类与EF CodeFirst一起使用,并且我想为实现ITypedEntity的每个类都有一个不同的表。

您需要将MagazineType更改为TypedEntity

public class Magazine:INamedEntity,ITypedEntity
{
public int Id {get;set;}
[MaxLength(250)]
public string Name {get;set;}
public TypedEntity Type { get; set; }
}
但当您创建杂志的对象时,可以为其指定派生类型

var magazine = new Magazine { Type = new MagazineType() }

MagazineType
TypedEntity
并不重要。假设您有另一个
ITypedEntity
实现,
NewspaperType
。ITypedEntity接口的合同规定您必须能够:

ITypedEntity magazine = new Magazine();
magazine.Type = new NewspaperType();
但是,这与您的代码相矛盾,在您的代码中,
Magazine.Type
不接受
ITypedEntity
的其他子类型。(我相信如果属性只有一个getter,那么您的代码将是有效的。)

解决方案是使用泛型:

interface ITypedEntity<TType> where TType : TypedEntity
{
    TType Type { get; set; }
}

class Magazine : ITypedEntity<MagazineType>
{
    // ...
}
接口ITypedEntity,其中TType:TypedEntity
{
t类型类型{get;set;}
}
班级杂志:ITypedEntity
{
// ...
}

要实现这一点,您必须添加一个
ITypedEntity.Type

public class Magazine:INamedEntity,ITypedEntity
{
    public int Id {get;set;}
    [MaxLength(250)]
    public string Name {get;set;}
    public MagazineType Type { get; set; }

    TypedEntity ITypedEntity.Type 
    {
        get
        {
            return this.Type;
        } 
        set 
        {
            this.Type = value as MagazineType; // or throw an exception if value
                                               // is not a MagazineType 
        }
    }
}
用法:

Magazine mag = new Magazine();
mag.Type                 \\ points to `public MagazineType Type { get; set; }`
((ITypedEntity)mag).Type \\ point to `TypedEntity ITypedEntity.Type`

MagazineType是TypedEntity,但TypedEntity不是MagazineType。也许您可以像
ITypedEntity类型{get;set;}那样定义接口
但是,如果代码引用了存储在类型为
ITypedEntity
的变量中的
杂志
,则可能会尝试将非
MagazineType
的内容存储到
类型
属性中。请将
杂志.type
的返回类型更改为
EntityType
。它仍然可以是
MagazineType
的一个实例,但签名必须匹配以满足接口实现。我知道这是可行的,但问题是这是我打算与EF CodeFirst一起使用的一些模型,我不确定这将如何工作,因为我想为TypedEntity的每个类conrect实现提供一个不同的表