返回列表中的具体实现

发布于 2024-12-06 07:24:21 字数 408 浏览 6 评论 0原文

我知道这不能编译,但为什么不应该编译呢?

public interface IReportService {
  IList<IReport> GetAvailableReports();
  IReport GetReport(int id);
}

public class ReportService : IReportService {
 IList<IReport> GetAvailableReports() {
   return new List<ConcreteReport>(); // This doesnt work
 }

 IReport GetReport(int id){
   return new ConcreteReport(); // But this works
 }
}

I know this doesnt compile but why shouldnt it?

public interface IReportService {
  IList<IReport> GetAvailableReports();
  IReport GetReport(int id);
}

public class ReportService : IReportService {
 IList<IReport> GetAvailableReports() {
   return new List<ConcreteReport>(); // This doesnt work
 }

 IReport GetReport(int id){
   return new ConcreteReport(); // But this works
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱情眠于流年 2024-12-13 07:24:21

这是因为协方差。您可以让它在 .NET 4 中工作(阅读链接)。

It's because of covariance. You can get it to work in .NET 4 (read the link).

娇女薄笑 2024-12-13 07:24:21

尝试更改为这个

IList<? extends IReport> GetAvailableReports()

Try change to this

IList<? extends IReport> GetAvailableReports()
怀念你的温柔 2024-12-13 07:24:21

我最近自己也遇到了这个问题,发现使用 IEnumerable 而不是 List 可以解决问题。这是一个相当令人沮丧的问题,但是一旦我找到了问题的根源,它就有意义了。

这是我用来找到解决方案的测试代码:

using System.Collections.Generic;

namespace InheritList.Test
{
    public interface IItem
    {
        string theItem;
    }

    public interface IList
    {
        IEnumerable<IItem> theItems; // previously has as list... didn't work.
                                     // when I changed to IEnumerable, it worked.
        public IItem returnTheItem();
        public IEnumerable<IItem> returnTheItemsAsList();
    }

    public class Item : IItem
    {
        string theItem;
    }

    public class List : IList
    {
        public IEnumerable<IItem> theItems; // List here didn't work - changed to IEnumerable

        public List()
        {
            this.theItems = returnTheItemsAsList();
        }
        public IItem returnTheItem()
        {
            return new Item();
        }

        public IEnumerable<IItem> returnTheItemsAsList()
        {
            var newList = new List<Item>();
            return newList;
        }
    }
}

I recently ran into this problem myself, and found that using IEnumerable instead of List solves the problem. It was quite a frustrating issue, but once I found the source of the problem, it made sense.

Here's the test code I used to find the solution:

using System.Collections.Generic;

namespace InheritList.Test
{
    public interface IItem
    {
        string theItem;
    }

    public interface IList
    {
        IEnumerable<IItem> theItems; // previously has as list... didn't work.
                                     // when I changed to IEnumerable, it worked.
        public IItem returnTheItem();
        public IEnumerable<IItem> returnTheItemsAsList();
    }

    public class Item : IItem
    {
        string theItem;
    }

    public class List : IList
    {
        public IEnumerable<IItem> theItems; // List here didn't work - changed to IEnumerable

        public List()
        {
            this.theItems = returnTheItemsAsList();
        }
        public IItem returnTheItem()
        {
            return new Item();
        }

        public IEnumerable<IItem> returnTheItemsAsList()
        {
            var newList = new List<Item>();
            return newList;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文