在使用我创建的界面时遇到麻烦。我尝试实现它,但是发生错误。任何答案表示赞赏。提前致谢。
这是我要实现的实际接口。
namespace CRDM.Core.Models
{
[Table("cities")]
public class City : ICity<CountryState>
{
}
[Table("country_states")]
public class CountryState : ICountryState<Country>
{
}
[Table("countries")]
public class Country : ICountry
{
}
}
namespace CRDM.Core.Abstractions.Entities
{
public interface ICity <TState> :
where TState : ICountryState<ICountry>
{
TState StateReference { get; set; }
}
public interface ICountryState<TCountry> :
where TCountry : ICountry
{
}
public interface ICountry
{
}
}
我成功实现了Country
和CountryState
类,但是实现City
时出错。错误消息在这里。
类型
CRDM.Core.Models.CountryState
不能用作类型 通用类型或方法TState
中的参数ICity<TState>
。没有来自的隐式引用转换
CRDM.Core.Models.CountryState
至CRDM.Core.Abstractions.Entities.ICountryState<CRDM.Core.Abstractions.Entities.ICountry>
。
答案 0 :(得分:1)
尝试通过这种方式进行操作:
namespace CRDM.Core.Models
{
public class City : ICity<CountryState,Country>
{
public CountryState StateReference { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
public class CountryState : ICountryState<Country>
{
}
public class Country : ICountry
{
}
}
namespace CRDM.Core.Abstractions.Entities
{
public interface ICity<TState,TCountry>
where TCountry: ICountry
where TState : ICountryState<TCountry>
{
TState StateReference { get; set; }
}
public interface ICountryState<TCountry>
where TCountry : ICountry
{
}
public interface ICountry
{
}
}
或者这样:
namespace CRDM.Core.Models
{
public class City : ICity<CountryState>
{
public CountryState StateReference { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
}
public class CountryState : ICountryState<ICountry>
{
}
public class Country : ICountry
{
}
}
namespace CRDM.Core.Abstractions.Entities
{
public interface ICity<TState>
where TState : ICountryState<ICountry>
{
TState StateReference { get; set; }
}
public interface ICountryState<TCountry>
where TCountry : ICountry
{
}
public interface ICountry
{
}
}
答案 1 :(得分:0)
您可能需要问自己,您要达到什么目标,是否只是使事情变得比原来复杂得多。如果您想使用接口访问这些实体,但使它们与实体框架一起使用,我会...
namespace CRDM.Core.Models
{
using CRDM.Core.Abstractions.Entities;
[Table("cities")]
public class City : ICity
{
public CountryState StateReference { get; set; }
ICountryState ICity.StateReference
{
get
{
return StateReference;
}
set
{
StateReference = (CountryState)value;
}
}
}
[Table("country_states")]
public class CountryState : ICountryState
{
public Country Country { get; set; }
ICountry ICountryState.Country
{
get
{
return Country;
}
set
{
Country = (Country)value;
}
}
}
[Table("countries")]
public class Country : ICountry
{
}
}
namespace CRDM.Core.Abstractions.Entities
{
public interface ICity
{
ICountryState StateReference { get; set; }
}
public interface ICountryState
{
ICountry Country { get; set; }
}
public interface ICountry
{
}
}