“无法转换为 IComparer”

发布于 2024-08-06 21:54:33 字数 786 浏览 7 评论 0 原文

我为装箱的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>,但这超出了我的控制范围) 。

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

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

I have the following IComparer defined for boxed RegistryItem objects:

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);
    }
}

I want to use this to sort an ArrayList of boxed RegistryItems (It really should be a List<RegistryItem>, but that's out of my control).

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

However, the last line gives the compiler error: "Cannot convert from BoxedRegistryItemComparer to System.Collections.IComparer". I would appreciate it if someone could point out my mistake.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

-残月青衣踏尘吟 2024-08-13 21:54:33

BoxedRegistryItemComparer 应该实现 System.Collections.IComparer 以便与 ArrayList.Sort 一起使用。您实现了 System.Collections.Generic.IComparer ,这不是同一件事。

BoxedRegistryItemComparer should implement System.Collections.IComparer to be used with ArrayList.Sort. you implemented System.Collections.Generic.IComparer<T> which is not the same thing.

左耳近心 2024-08-13 21:54:33

您已定义通用比较器 (IComparer;) 而不是 没有类型的比较器(IComparerArrayList.Sort() 需要一个非通用的 IComparer

泛型类型不能转换为其非泛型等价物。

You've defined a Generic-Comparer (IComparer<T>) instead of a Comparer without a type (IComparer). ArrayList.Sort() expects a non-generic IComparer.

Generic-Types can not be casted into their non-generic equivalents.

那一片橙海, 2024-08-13 21:54:33

如果您无法控制比较器或排序器,这里有两个迷你类可以在两种类型之间进行转换(未经测试):

private class GenericComparer<T> : IComparer<T>
{
    IComparer _Comparer;
    public GenericComparer(IComparer comparer)
    {
        _Comparer = comparer;
    }
    public int Compare(T a, T b)
    {
        return _Comparer.Compare(a, b);
    }
}

private class NonGenericComparer<T> : IComparer
{
    IComparer<T> _Comparer;
    public NonGenericComparer(IComparer<T> comparer)
    {
        _Comparer = comparer;
    }
    public int Compare(object a, object b)
    {
        return _Comparer.Compare((T)a, (T)b);
    }
}

In case you have a situation where you don't have any control over the Comparer or the Sorter, here are two mini-classes which can convert between the two types (untested):

private class GenericComparer<T> : IComparer<T>
{
    IComparer _Comparer;
    public GenericComparer(IComparer comparer)
    {
        _Comparer = comparer;
    }
    public int Compare(T a, T b)
    {
        return _Comparer.Compare(a, b);
    }
}

private class NonGenericComparer<T> : IComparer
{
    IComparer<T> _Comparer;
    public NonGenericComparer(IComparer<T> comparer)
    {
        _Comparer = comparer;
    }
    public int Compare(object a, object b)
    {
        return _Comparer.Compare((T)a, (T)b);
    }
}
混吃等死 2024-08-13 21:54:33

也许它是一个拐杖,但它确实有效:

            arrayList.Sort(Comparer<object>.Create(
            (x, y) => ((RegistryItem)x).Name.CompareTo(((RegistryItem)y).Name)));

Perhaps it is a crutch, but it works:

            arrayList.Sort(Comparer<object>.Create(
            (x, y) => ((RegistryItem)x).Name.CompareTo(((RegistryItem)y).Name)));
糖粟与秋泊 2024-08-13 21:54:33

上述帖子的替代方法是让您的比较器类实现这两个接口,然后您可以将 IComparer 强制转换为如果您两者都需要,请转到 IComparer。

public class MyComparer<T> : IComparer<T>, IComparer

An alternative to above post would be to have your comparer class implement both interfaces, then you can cast IComparer<T> to IComparer should you need both.

public class MyComparer<T> : IComparer<T>, IComparer
夜声 2024-08-13 21:54:33
public class BoxedRegistryItemComparer : IComparer {
...
}
public class BoxedRegistryItemComparer : IComparer {
...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文