“无法转换为 IComparer”

2024-04-19

我为装箱的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进行排序(它实际上应该是一个列表<RegistryItem>,但这超出了我的控制范围)。

ArrayList regItems = new ArrayList();
// fill up the list ...
BoxedRegistryItemComparer comparer = new BoxedRegistryItemComparer();
ArrayList.sort(comparer);

但是,最后一行给出了编译器错误:“无法从 BoxedRegistryItemComparer 转换为 System.Collections.IComparer”。如果有人能指出我的错误,我将不胜感激。


BoxedRegistryItemComparer应该实施System.Collections.IComparer与使用ArrayList.Sort。你实施了System.Collections.Generic.IComparer<T>这不是同一件事。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“无法转换为 IComparer” 的相关文章

随机推荐