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

时间:2012-08-14 15:22:20

标签: c# generics inheritance interface

我有接口IChildIParentIParent的成员是List<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; }
}

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

`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>`.

5 个答案:

答案 0 :(得分:32)

使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; }
}

答案 1 :(得分:2)

您需要让班级返回List<IChild>

public class ParentA : IParent
{ 
    public List<IChild> a { get; set; }
}

public class ParentB : IParent
{ 
    public List<IChild> a { get; set; }
}

答案 2 :(得分:1)

实现只能返回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;
    }
}

答案 3 :(得分:0)

IChild的集合无法隐式转换为其子类型

的集合

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

答案 4 :(得分:0)

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

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

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

简单模型。

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

复杂模型。

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

常见的业务逻辑和示例调用。这里的关键部分是使用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();
        }
    }
}
相关问题