我有一个Event带有DateTime成员的基类TimeStamp.许多其他事件类将源于此.
我希望能够快速搜索事件列表,所以我想使用二进制搜索.
(列表数据按时间戳排序,但同时发生的事件可能存在重复的时间戳)
所以我开始写这样的东西:
public class EventList<T> : List<T> where T : Event
{
private IComparer<T> comparer = (x, y) => Comparer<DateTime>.Default.Compare(x.TimeStamp, y.TimeStamp);
public IEnumerable<T> EventsBetween(DateTime inFromTime, DateTime inToTime)
{
// Find the index for the beginning.
int index = this.BinarySearch(inFromTime, comparer);
// BLAH REST OF IMPLEMENTATION
}
}
Run Code Online (Sandbox Code Playgroud)
问题是BinarySearch只接受T(所以 - Event类型)作为参数,而我想基于T 的成员 - TimeStamp进行搜索.
什么是一个很好的方法来解决这个问题?