简体   繁体   English

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

[英]How to use interface with generic object?

I'm getting trouble in using the interface I create.我在使用我创建的界面时遇到了麻烦。 I try to implement it but there an error occur.我尝试实现它,但出现错误。 Any answer is appreciated.任何答案表示赞赏。 Thanks in advance.提前致谢。

Here the actual Interface I want to implement.这里是我想要实现的实际接口。

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

I successfully implement the Country and CountryState class, but there an error in the implementation of City .我成功地实现了CountryCountryState类,但是在City的实现中出现了错误。 Here the error message.这里是错误信息。

The type CRDM.Core.Models.CountryState cannot be used as type parameter TState in the generic type or method ICity<TState> .类型CRDM.Core.Models.CountryState不能用作泛型类型或方法ICity<TState>中的类型参数TState

There is no implicit reference conversion from CRDM.Core.Models.CountryState to CRDM.Core.Abstractions.Entities.ICountryState<CRDM.Core.Abstractions.Entities.ICountry> .没有从CRDM.Core.Models.CountryStateCRDM.Core.Abstractions.Entities.ICountryState<CRDM.Core.Abstractions.Entities.ICountry>隐式引用转换。

Try doing it this way:尝试这样做:

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

Or this way:或者这样:

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

You probably need to ask yourself what are you trying to achieve and are you simply making this more complicated than it needs to be.你可能需要问问自己你想要达到什么目标,你是否只是让这比需要的更复杂。 If you want to access these entities using interfaces but make them work with entity framework I would do...如果你想使用接口访问这些实体,但让它们与实体框架一起工作,我会做......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM