【问题标题】:How to use interface with generic object?如何将接口与通用对象一起使用?
【发布时间】:2018-11-13 12:22:42
【问题描述】:

我在使用我创建的界面时遇到了问题。我尝试实现它,但出现错误。任何答案表示赞赏。提前致谢。

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

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

我成功实现了CountryCountryState类,但是City的实现出现了错误。这里是错误信息。

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

没有隐式的引用转换 CRDM.Core.Models.CountryStateCRDM.Core.Abstractions.Entities.ICountryState&lt;CRDM.Core.Abstractions.Entities.ICountry&gt;.

【问题讨论】:

  • 恐怕您尝试使用泛型的方式有问题。看起来您正在考虑为不同的国家或州开设不同的课程。或者,也许您正试图使用​​通用参数作为将城市与州与国家联系起来的一种方式,等等。我可能完全错了。但如果是这种情况,那么最好退后一步,看看是否有不同的方法来做你想做的事情。
  • 这是泛型的一个常见错误:您认为ICountryState&lt;Country&gt; 可以转换为ICountryState&lt;ICountry&gt;,但它不能。

标签: c# generics interface


【解决方案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
        {
        }
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 2017-04-16
      相关资源
      最近更新 更多