2012-08-21 3 views
0

나는 투표를 한 다음 정렬 할 수있는 여러 클래스를 정의하는 추상 클래스가 있습니다. 이 클래스들은 모두 정렬 된 속성을 공유하기 때문에 이러한 속성으로 정렬 할 수있는 추상 수준의 메서드를 포함하고 싶습니다. 그러나 '매개 변수에 할당 할 수 없음'오류가 발생합니다. .추상 클래스의 객체 컬렉션에 정렬 메서드 사용

internal abstract class ESCO 
{ 
    public double HotScore { get; set; } 
    public double VoteTotal { get; set; } 
    public DateTime Created { get; set; } 

    protected static List<ESCO> SortedItems(List<ESCO> escoList, ListSortType sortType) 
    { 
     switch (sortType) 
     { 
      case ListSortType.Hot: 
       escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.HotScore.CompareTo(p1.HotScore); }); 
       return escoList; 
      case ListSortType.Top: 
       escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.VoteTotal.CompareTo(p1.VoteTotal); }); 
       return escoList; 
      case ListSortType.Recent: 
       escoList.Sort(delegate(ESCO p1, ESCO p2) { return p2.Created.CompareTo(p1.Created); }); 
       return escoList; 
      default: 
       throw new ArgumentOutOfRangeException("sortType"); 
     } 
    } 
    private SPUser GetCreatorFromListValue(SPListItem item) 
    { 
     var user = new SPFieldUserValue(SPContext.Current.Web, (string)item["Author"]); 
     return user.User; 
    } 
    private static VoteMeta InformationForThisVote(List<Vote> votes, int itemId) 
    {} // There are more methods not being shown with code to show why I used 
     // abstract instead of something else 
} 

같은 구현하는 시도 :

가 어떻게 다음 처리해야

class Post : ESCO 
{ 
    public string Summary { get; set; } // Properties in addition to abstract 
    public Uri Link { get; set; } // Properties in addition to abstract 

    public static List<Post> Posts(SPListItemCollection items, ListSortType sortType, List<Vote> votes) 
    { 
     var returnlist = new List<Post>(); 
     for (int i = 0; i < items.Count; i++) { returnlist.Add(new Post(items[i], votes)); } 
     return SortedItems(returnlist, sortType); 
    } 

나는 완전히 개방 나는 것은 "모든 잘못하고있어"합니다.

+0

정확히 *는 오류지고있다, 그리고 당신이 그것을 어디에서 무엇을 얻을 수 있습니까 * 정확히 * *인가? –

+0

마지막에 return 문이 있습니다. esco를 매개 변수로 할당 할 수 없게하기. – Wesley

+0

인터페이스를 사용하여 기본 클래스 대신 정렬 할 수있는 속성을 정의 할 수 있습니다. 나는 상속이 "있는 것"관계라고 생각하고 인터페이스는 "할 수있다"물건을 정렬하는 데 사용됩니다. –

답변

2

나는 같은 오류 메시지를 재현 할 수없는,하지만 난

return SortedItems(returnlist, sortType); 

는 추상 기본 클래스의 목록을 반환하려고 때문에 점점 오류라고 생각합니다. 당신은 아직 작성하지 않은 경우 을 System.Linq 네임 스페이스를 포함해야합니다

return SortedItems(returnlist, sortType).Cast<Post>().ToList(); 

에 그 변경하십시오.

참고로, 오류 나는 (단순화 된 테스트 케이스에) 수는

Cannot implicitly convert type System.Collections.Generic.List<MyNamespace.ESCO>' to 'System.Collections.Generic.List<MyNamespace.Post>'

관련 문제