无法实现接口成员,因为它没有List< IInterface>的匹配返回类型。 [英] Cannot implement interface member because it does not have the matching return type of List<IInterface>

查看:826
本文介绍了无法实现接口成员,因为它没有List< IInterface>的匹配返回类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有接口 IChild IParent IParent 的成员是列表< IChild>

I have interfaces IChild and IParent. IParent has a member that is a List<IChild>.

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

I wish to have classes that implement IParent where each class has a member that implements 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>`.


推荐答案

使IParent通用:

Make IParent generic:

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

这篇关于无法实现接口成员,因为它没有List&lt; IInterface&gt;的匹配返回类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆