如何实现包含接口的接口?
How to implement an interface that contains an interface?
我有两个接口:
public interface IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ILocation LocationOfBirth { get; set; }
}
public interface ILocation
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
现在我想这样实现它们:
public class Location : ILocation
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Location LocationOfBirth { get; set; }
}
但是c#对我很生气:
Property 'LocationOfBirth' cannot implement property from interface 'IPerson'. Type should be 'ILocation'.
当 Location 满足 ILocation 的所有要求时,为什么会这样?
我想使用 Person 作为 EntityFramework 的模型,所以我不能使用 ILocation。怎么办?
编辑:在我的应用程序中,接口的实现不在接口的范围内,所以我不能用 Location 实现定义 IPerson。
如果您想在 EF 中使用 class,请尝试此代码。 EF 将有一个具体的 class 版本,如果您需要其他地方的接口,它也可以工作。
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
[NotMapped]
public ILocation LocationOfBirth
{
get {return BirthLocation;}
set {BirthLocation= (Location) value;
}
public Location BirthLocation {get; set;}
}
C# 预编译器将 ILocation 解析为具体的 class 在使用过程中实例化或赋值的位置
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ILocation LocationOfBirth { get; set; }
}
如果使用显式接口实现,这可以很容易地完成:
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Location LocationOfBirth { get; set; }
// Explicit interface implementation here
ILocation IPerson.LocationOfBirth {
get => this.LocationOfBirth;
set => this.LocationOfBirth = value as Location;
}
}
我有两个接口:
public interface IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ILocation LocationOfBirth { get; set; }
}
public interface ILocation
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
现在我想这样实现它们:
public class Location : ILocation
{
public double Latitude { get; set; }
public double Longitude { get; set; }
}
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Location LocationOfBirth { get; set; }
}
但是c#对我很生气:
Property 'LocationOfBirth' cannot implement property from interface 'IPerson'. Type should be 'ILocation'.
当 Location 满足 ILocation 的所有要求时,为什么会这样?
我想使用 Person 作为 EntityFramework 的模型,所以我不能使用 ILocation。怎么办?
编辑:在我的应用程序中,接口的实现不在接口的范围内,所以我不能用 Location 实现定义 IPerson。
如果您想在 EF 中使用 class,请尝试此代码。 EF 将有一个具体的 class 版本,如果您需要其他地方的接口,它也可以工作。
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
[NotMapped]
public ILocation LocationOfBirth
{
get {return BirthLocation;}
set {BirthLocation= (Location) value;
}
public Location BirthLocation {get; set;}
}
C# 预编译器将 ILocation 解析为具体的 class 在使用过程中实例化或赋值的位置
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public ILocation LocationOfBirth { get; set; }
}
如果使用显式接口实现,这可以很容易地完成:
public class Person : IPerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Location LocationOfBirth { get; set; }
// Explicit interface implementation here
ILocation IPerson.LocationOfBirth {
get => this.LocationOfBirth;
set => this.LocationOfBirth = value as Location;
}
}