2011-09-22 3 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

이 때문에 covariance의입니다. .NET 4에서 작동하도록 할 수 있습니다 (링크 읽기).

0

보십시오 변화는

IList<? extends IReport> GetAvailableReports() 
0

최근에 나는이 문제에 직접 직면했으며 List 대신 IEnumerable을 사용하면 문제가 해결된다는 것을 알게되었습니다. 상당히 실망한 문제 였지만 문제의 원인을 찾았 으면 이치에 맞았습니다. 당신이 더 많은 답을 얻기 위해서 C#을 태그를 추가 할 수 있습니다

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