2011-09-22 37 views
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 
} 
} 
+0

。 – jgauffin

回答

0

尝试改变这种

IList<? extends IReport> GetAvailableReports() 
0

我最近自己遇到了这个问题,发现使用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; 
     } 
    } 
} 
你可能要添加C#的标签,以获得更多的答案
相关问题