无法实现接口成员,因为它没有匹配的返回类型List

我有接口IChildIParentIParent的成员是List

我希望有一些实现IParent类,其中每个类都有一个实现IChild的成员:

 public interface IChild { } public interface IParent { List a { get; set; } } public class ChildA : IChild { } public class ChildB : IChild { } public class ParentA : IParent { public List a { get; set; } } public class ParentB : IParent { public List a { get; set; } } 

但是,这段代码不会编译。 错误是:

 `MyApp.Data.ParentA` does not implement interface member `MyApp.Data.IParent.a`. `MyApp.Data.ParentA.a` cannot implement `MyApp.Data.IParent.a` because it does not have the matching return type of `System.Collections.Generic.List`. 

使IParent通用:

 public interface IChild { } public interface IParent where TChild : IChild { List a { get; set; } } public class ChildA : IChild { } public class ChildB : IChild { } public class ParentA : IParent { public List a { get; set; } } public class ParentB : IParent { public List a { get; set; } } 

您需要让类返回List

 public class ParentA : IParent { public List a { get; set; } } public class ParentB : IParent { public List a { get; set; } } 

该实现只能返回IChild List,如下所示:

 public interface IChild { } public interface IParent { List Children { get; set; } } public class ChildA : IChild { } public class ChildB : IChild { } public class ParentA : IParent { public List Children { get; set; } } public class ParentB : IParent { public List Children { get; set; } } 

IChild的集合不能隐式转换为其子类型的集合

IParent.a的返回类型更改为List ParentAParentB上的属性声明更改为public List a { get; set; } public List a { get; set; } public List a { get; set; } 。 我推荐后者,因为我认为这是你最有可能的。

我有一个类似的要求,我有两种不同的方法,它们在两个不同的类上运行,但在它们中对于两个类共有的属性具有相同的逻辑。

所以我想用inheritance和generics来编写一个通用的方法,我能够通过以下方式实现。

 namespace OOPS.Interfaces { using System.Collections.Generic; public interface IBanner { string Name { get; set; } } public interface IBannerContent where T : IBanner { List Banners { get; set; } } } 

简单模型。

 namespace OOPS.Simple { using Interfaces; using System.Collections.Generic; public class Banner : IBanner { public string Name { get; set; } } public class BannerContent : IBannerContent { public List Banners { get; set; } } } 

复杂模型。

 namespace OOPS.Complex { using Interfaces; using System.Collections.Generic; public class Banner : IBanner { public string Name { get; set; } public string Description { get; set; } } public class BannerContent : IBannerContent { public List Banners { get; set; } } } 

常见的业务逻辑和示例调用。 这里的关键部分是使用where子句来约束类型,例如where T : IBanner一直向下直到我们希望它为常见的方法。

 namespace OOPS { using Interfaces; using System; using System.Collections.Generic; public class BusinessLogic { public void Print(IBannerContent bannerContent) where T : IBanner { foreach (var item in bannerContent.Banners) { Console.WriteLine(item.Name); } } } class Program { static void Main(string[] args) { var banner1 = new Simple.BannerContent { Banners = new List { new Simple.Banner { Name = "Banner 1" }, new Simple.Banner { Name = "Banner 2" } } }; var banner2 = new Complex.BannerContent { Banners = new List { new Complex.Banner { Name = "Banner 3", Description = "Test Banner" }, new Complex.Banner { Name = "Banner 4", Description = "Design Banner" } } }; var business = new BusinessLogic(); business.Print(banner1); business.Print(banner2); Console.ReadLine(); } } }