我使用的是WPF博士的ObservableSortedDictionary。
构造函数如下所示:
public ObservableSortedDictionary(IComparer<DictionaryEntry> comparer)我真的在努力创建一个满足构造器和工作的实现。
我当前的代码(无法编译)是:
public class TimeCreatedComparer<T> : IComparer<T>
{
public int Compare(T x, T y)
{
var myclass1 = (IMyClass)((DictionaryEntry)x).Value;
var myclass2 = (IMyClass)((DictionaryEntry)y).Value;
return myclass1.TimeCreated.CompareTo(myclass2.TimeCreated);
}
}它说我不能从T转换到DictionaryEntry。
如果我直接转换为IMyClass,它会编译,但我得到一个运行时错误,告诉我无法从DictionaryEntry转换为IMyClass。在运行时,x和y是DictionaryEntry的实例,每个实例都有正确的IMyClass作为它们的值。
发布于 2010-04-16 03:45:17
public class TimeCreatedComparer : IComparer<DictionaryEntry>
{
public int Compare(DictionaryEntry x, DictionaryEntry y)
{
var myclass1 = (IMyClass)x.Value;
var myclass2 = (IMyClass)y.Value;
return myclass1.TimeCreated.CompareTo(myclass2.TimeCreated);
}
}这是否做了所需的工作?
https://stackoverflow.com/questions/2648442
复制相似问题