简体   繁体   English

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

[英]Cannot implement interface member because it does not have the matching return type of List<IInterface>

I have interfaces IChild and IParent . 我有接口IChildIParent 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> ParentAParentB的属性声明更改为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.

相关问题 无法实现接口成员,因为它没有匹配的返回类型 - Cannot implement interface member because it does not have the matching return type 无法实现接口成员,因为它没有匹配的返回类型 - unable to implement interface member because it does not have the matching return type 无法实现接口成员,因为它没有匹配的返回类型 - Cannot implement interface member because it doesn't have matching return type BaseClass 不能实现 interface.variable 因为它没有匹配的返回类型 - BaseClass cannot implement interface.variable because it does not have the matching return type 接口设计实现错误:…无法实现…,因为它没有匹配的返回类型 - Interface design implementation error: … cannot implement … because it does not have a matching return type 无法实现接口,因为任务没有匹配的返回类型<custom class>方法</custom> - Cannot implement interface because does not have matching returning type for Task<Custom class> method System.Windows.Window.Activate()无法实现Microsoft.Office.Interop.Word.Window.Activate(),因为它没有匹配的返回类型? - System.Windows.Window.Activate() cannot implement Microsoft.Office.Interop.Word.Window.Activate() because it does not have the matching return type? 通用类型的接口实现失败,因为“没有匹配的返回类型”错误 - Interface implementation fails for generic type, because of “does not have the matching return type” error “...”无法实现接口成员,因为它不是公共的 - “…” cannot implement an interface member because it is not public 无法实现接口成员,因为它不是公共的 - Cannot implement an interface member because it is not public
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM