C# 如何使用通用对象的接口?

C# 如何使用通用对象的接口?,c#,generics,interface,C#,Generics,Interface,我在使用我创建的界面时遇到了麻烦。我试图实现它,但出现了一个错误。任何答复都将不胜感激。提前谢谢 这里是我想要实现的实际接口 namespace CRDM.Core.Models { [Table("cities")] public class City : ICity<CountryState> { } [Table("country_states")] public class CountryState : ICountryState<Countr

我在使用我创建的界面时遇到了麻烦。我试图实现它,但出现了一个错误。任何答复都将不胜感激。提前谢谢

这里是我想要实现的实际接口

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
 {
 }
}
名称空间CRDM.Core.Models
{
[表(“城市”)]
公共类城市:城市
{
}
[表(“国家/州”)]
公共类CountryState:ICountryState
{        
}
[表(“国家”)]
公共类国家:ICountry
{       
}
}
命名空间CRDM.Core.Abstractions.Entities
{
公共接口:
其中TState:i取消系统状态
{
TState状态引用{get;set;}
}
公共接口i取消系统状态:
where TCountry:ICountry
{
}
公共界面ICountry
{
}
}
我成功地实现了
Country
CountryState
类,但是
City
的实现中出现了错误。这里是错误消息

类型
CRDM.Core.Models.CountryState
不能用作类型 泛型类型或方法中的参数
TState

不存在来自的隐式引用转换
CRDM.Core.Models.CountryState
to
CRDM.Core.Abstractions.Entities.iconystate

试着这样做:

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
        {
        }
    }
名称空间CRDM.Core.Models
{
公共类城市:城市
{
公共CountryState状态引用{get=>throw new NotImplementedException();set=>throw new NotImplementedException();}
}
公共类CountryState:ICountryState
{
}
公共类国家:ICountry
{
}
}
命名空间CRDM.Core.Abstractions.Entities
{
公共接口
where TCountry:ICountry
其中TState:i取消系统状态
{
TState状态引用{get;set;}
}
公共接口i取消系统状态
where TCountry:ICountry
{
}
公共界面ICountry
{
}
}
或者这样:

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
        {
        }
    }
名称空间CRDM.Core.Models
{
公共类城市:城市
{
公共CountryState状态引用{get=>throw new NotImplementedException();set=>throw new NotImplementedException();}
}
公共类CountryState:ICountryState
{
}
公共类国家:ICountry
{
}
}
命名空间CRDM.Core.Abstractions.Entities
{
公共接口
其中TState:i取消系统状态
{
TState状态引用{get;set;}
}
公共接口i取消系统状态
where TCountry:ICountry
{
}
公共界面ICountry
{
}
}

你可能需要问问自己,你想达到什么目的,你只是让事情变得比需要的更复杂。如果您想使用接口访问这些实体,但让它们与实体框架一起工作,我会这样做

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
    {
    }
}

恐怕你使用泛型的方式有问题。看起来你在考虑为不同的国家或州开设不同的课程。或者你想用一般性的论点把城市、州、国家等联系起来,我可能完全错了。但是如果是这样的话,那么退一步看看是否有不同的方法来做你正在尝试做的事情可能是好的。这是泛型的一个常见错误:你假设
iconystate
可以被转换为
iconystate
,但它不能。