[英]Cannot implement interface member because it does not have the matching return type of List<IInterface>
I have interfaces IChild
and IParent
. 我有接口
IChild
和IParent
。 IParent
has a member that is a List<IChild>
. IParent
的成员为List<IChild>
。
I wish to have classes that implement IParent
where each class has a member that implements IChild
: 我希望有一个实现
IParent
类,其中每个类都有一个实现IChild
的成员:
public interface IChild
{
}
public interface IParent
{
List<IChild> a { get; set; }
}
public class ChildA : IChild
{
}
public class ChildB : IChild
{
}
public class ParentA : IParent
{
public List<ChildA> a { get; set; }
}
public class ParentB : IParent
{
public List<ChildB> a { get; set; }
}
But, this code will not compile. 但是,此代码将无法编译。 The error is:
错误是:
`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<MyApp.Data.IChild>`.
Make IParent generic: 使IParent具有通用性:
public interface IChild
{
}
public interface IParent<TChild> where TChild : IChild
{
List<TChild> a { get; set; }
}
public class ChildA : IChild { }
public class ChildB : IChild { }
public class ParentA : IParent<ChildA>
{
public List<ChildA> a { get; set; }
}
public class ParentB : IParent<ChildB>
{
public List<ChildB> a { get; set; }
}
You need to have the classes return a List<IChild>
: 您需要让类返回
List<IChild>
:
public class ParentA : IParent
{
public List<IChild> a { get; set; }
}
public class ParentB : IParent
{
public List<IChild> a { get; set; }
}
The implementation can only return List of IChild as follows: 该实现只能返回IChild的List,如下所示:
public interface IChild
{
}
public interface IParent
{
List<IChild> Children { get; set; }
}
public class ChildA : IChild
{
}
public class ChildB : IChild
{
}
public class ParentA : IParent
{
public List<IChild> Children
{
get;
set;
}
}
public class ParentB : IParent
{
public List<IChild> Children
{
get;
set;
}
}
A collection of IChild
cannot be implicitly converted to a collection of its child type IChild
的集合不能隐式转换为其子类型的集合
Change the return type of IParent.a
to List<ChildA>
OR change the property declaration on ParentA
and ParentB
to public List<IChild> a { get; set; }
将
IParent.a
的返回类型更改为List<ChildA>
或将ParentA
和ParentB
的属性声明更改为public List<IChild> a { get; set; }
public List<IChild> a { get; set; }
public List<IChild> a { get; set; }
. public List<IChild> a { get; set; }
。 I recommend the latter, as I think that is what you're most likely going for. 我建议使用后者,因为我认为这很可能是您想要的。
I had a similar requirement where I had two different methods that operated on two different classes but had the same logic in it for the properties that are common to both the classes. 我有一个相似的要求,即我有两个对两个不同的类进行操作的不同方法,但对于两个类都具有相同的属性,它们具有相同的逻辑。
so I thought to use inheritance and generics for this to write a common method, I was able to achieve in the following way. 所以我想为此使用继承和泛型来编写一个通用方法,因此我可以通过以下方式实现。
namespace OOPS.Interfaces
{
using System.Collections.Generic;
public interface IBanner
{
string Name { get; set; }
}
public interface IBannerContent<T> where T : IBanner
{
List<T> Banners { get; set; }
}
}
Simple Model. 简单模型。
namespace OOPS.Simple
{
using Interfaces;
using System.Collections.Generic;
public class Banner : IBanner
{
public string Name { get; set; }
}
public class BannerContent : IBannerContent<Banner>
{
public List<Banner> Banners { get; set; }
}
}
Complex Model. 复杂模型。
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<Banner>
{
public List<Banner> Banners { get; set; }
}
}
The common business logic and sample invocation. 通用的业务逻辑和样本调用。 The key part here is using the
where
clause to constrain the type such as where T : IBanner
all the way down till the method we want it to be common. 这里的关键部分是使用
where
子句来约束类型,例如where T : IBanner
一直到我们希望它变得通用的方法为止。
namespace OOPS
{
using Interfaces;
using System;
using System.Collections.Generic;
public class BusinessLogic
{
public void Print<T>(IBannerContent<T> 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<Simple.Banner>
{
new Simple.Banner { Name = "Banner 1" },
new Simple.Banner { Name = "Banner 2" }
}
};
var banner2 = new Complex.BannerContent
{
Banners = new List<Complex.Banner>
{
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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.