我有一个抽象类,它定义了许多可以投票然后排序的类。由于这些类都共享它们排序的属性,我想在抽象级别包含一个方法,让我可以按这些属性对它们进行排序,但是我遇到了“不可分配给参数”错误的麻烦.
我应该如何处理以下情况:
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);
}
我对“你做错了”完全持开放态度。