我为盒装的 RegistryItem 对象定义了以下 IComparer:
public class BoxedRegistryItemComparer : IComparer<object>
{
public int Compare(object left, object right)
{
RegistryItem leftReg = (RegistryItem)left;
RegistryItem rightReg = (RegistryItem)right;
return string.Compare(leftReg.Name, rightReg.Name);
}
}
我想用它来对盒装 RegistryItems 的 ArrayList 进行排序(它确实应该是一个 List <RegistryItem
>,但这不在我的控制范围内)。
ArrayList regItems = new ArrayList();
// fill up the list ...
BoxedRegistryItemComparer comparer = new BoxedRegistryItemComparer();
ArrayList.sort(comparer);
但是,最后一行给出了编译器错误:“无法从 BoxedRegistryItemComparer 转换为 System.Collections.IComparer”。如果有人能指出我的错误,我将不胜感激。