0

为了观察 System.Array.BinarySearch 中键的比较器的调用(与涉及调用数组元素的比较器的典型场景相反),我们设置了以下测试:

class Many 
{
    public string data { get; set; }
}
class One : Many, IComparable
{
    public int CompareTo(object arg)
    {
        Many other = arg as Many;
        Console.WriteLine("Comparator of One invoked from: " + this.data);
        if (this.data.Length < other.data.Length) return -1;
        if (this.data.Length > other.data.Length) return 1;
        return 0;
    }
}

当我运行以下命令时,使用这些新声明:

   Many[] manies = new[] { new Many { data = "1" }, 
                           new Many { data = "22" }, 
                           new Many { data = "333" }, 
                           new Many { data = "4444" }, };
   One one = new One {data="???"};
   Console.WriteLine(Array.BinarySearch(manies, one));

我得到输出:

Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.ArgumentException: At least one object must implement IComparable.
   at System.Collections.Comparer.Compare(Object a, Object b)
   at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
   at System.Collections.Generic.ArraySortHelper`1.InternalBinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Collections.Generic.ArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   --- End of inner exception stack trace ---
   at System.Collections.Generic.ArraySortHelper`1.BinarySearch(T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Array.BinarySearch[T](T[] array, Int32 index, Int32 length, T value, IComparer`1 comparer)
   at System.Array.BinarySearch[T](T[] array, T value)
   at ConsoleApplication1.Prosram.Main(String[] args) in C:\Users\xxx\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line xxx
4

1 回答 1

3

的行为一直存在问题,BinarySearch即键的比较器从未实际执行过,即使 MSDN 文档说如果数组元素没有实现IComparable. 此问题已在 .NET 4.5 版中得到解决。

错误的堆栈跟踪中有一些线索显示了源自System.Collections.Comparer.Compare(). 查看 .NET 4.5 和 .NET 4.0 中此方法的 .NET 源代码,可以看出此方法的实现方式发生了重要变化:

.NET 4.5:

public int Compare(Object a, Object b) {
    if (a == b) return 0;
    ...
    IComparable ia = a as IComparable; 
    if (ia != null)
        return ia.CompareTo(b); 

    IComparable ib = b as IComparable;
    if (ib != null) 
        return -ib.CompareTo(a);

    throw new ArgumentException(Environment.GetResourceString(
                                       "Argument_ImplementIComparable"));

.NET 4.0:

public int Compare(Object a, Object b) {
    if (a == b) return 0;
    ...
    IComparable ia = a as IComparable; 
    if (ia != null)
        return ia.CompareTo(b); 

    throw new ArgumentException(Environment.GetResourceString(
                                      "Argument_ImplementIComparable"));

在我们的例子中,我们希望观察到键的比较器正在被调用,在这种情况下,键是b数组中的元素a

这意味着 BinarySearch 方法的 MSDN 上的文档对于 4.5 之前的 .NET 版本是不正确的。Comparator如果数组中的元素没有实现,则该方法不会考虑键的IComparable,因为实现取决于Comparer实际执行该操作并且它不执行该操作,ArgumentException而是抛出一个。但是,在 4.5 版中,它确实像BinarySearch.

于 2013-10-11T13:37:39.943 回答