0

我有一个DataGridView填充了 的组件SortableBindingList,如 SQL Server SDK 中的Microsoft.SqlServer.Management.Controls. 此类也用作 XML 序列化的根节点。

列表的成员属于具有两个原始字段和一个对象字段的类型。包含原始值的列按预期排序。但是,在对包含对象字段的列进行排序时,会引发以下异常:

System.InvalidOperationException was unhandled
  Message=Failed to compare two elements in the array.
  Source=mscorlib
  StackTrace:
       at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)
       at System.Array.Sort[T](T[] array, Int32 index, Int32 length, IComparer`1 comparer)
       at System.Collections.Generic.List`1.Sort(Comparison`1 comparison)
       at Microsoft.SqlServer.Management.Controls.SortableBindingList`1.ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
       at System.ComponentModel.BindingList`1.System.ComponentModel.IBindingList.ApplySort(PropertyDescriptor prop, ListSortDirection direction)
       at System.Windows.Forms.BindingSource.ApplySort(PropertyDescriptor property, ListSortDirection sort)
       ...
  InnerException: System.ArgumentException
       Message=At least one object must implement IComparable.
       Source=mscorlib
       StackTrace:
            at System.Collections.Comparer.Compare(Object a, Object b)
            at Microsoft.SqlServer.Management.Controls.SortableBindingList`1.<>c__DisplayClass1.<GetComparisionDelegate>b__0(T t1, T t2)
            at System.Array.FunctorComparer`1.Compare(T x, T y)
            at System.Collections.Generic.ArraySortHelper`1.SwapIfGreaterWithItems(T[] keys, IComparer`1 comparer, Int32 a, Int32 b)
            at System.Collections.Generic.ArraySortHelper`1.QuickSort(T[] keys, Int32 left, Int32 right, IComparer`1 comparer)
            at System.Collections.Generic.ArraySortHelper`1.Sort(T[] keys, Int32 index, Int32 length, IComparer`1 comparer)

从堆栈跟踪中可以清楚地看出,正在比较的对象不是IComparable——除了它们是。或者至少他们应该是。有问题的课程如下:

using System;
using System.Xml.Serialization;

namespace myapp.xmlobjects {
    [XmlType("error_after")]
    public class ErrorAfterObject : IComparable<ErrorAfterObject> {

        [XmlAttribute("hours")]
        public int Hours { get; set; }

        [XmlAttribute("minutes")]
        public int Minutes { get; set; }

        public ErrorAfterObject() { }

        public ErrorAfterObject(int hours, int minutes) {
            this.Hours = hours;
            this.Minutes = minutes;
        }

        public override string ToString() {
            return string.Format("{0} hr {1} min", this.Hours, this.Minutes);
        }

        public int CompareTo(ErrorAfterObject other) {
            return (this.Hours*60 + this.Minutes).CompareTo(other.Hours*60 + other.Minutes);
        }
    }
}

作为健全性检查,我在数据绑定后添加了以下调试代码:

Console.WriteLine(myGridView.Rows[0].Cells[2].ValueType.ToString());

myapp.xmlobjects.ErrorAfterObject正如我所期望的那样回来。

可能有助于解决问题的三个问题:我的对象是否被键入为无法比较的东西?是否可以准确检查正在比较的对象类型?我在IComparable实施过程中遗漏了什么吗?

提前致谢。

4

1 回答 1

2

事实证明,IComparableIComparable<T>不是一回事。将类定义替换为:

public class ErrorAfterObject : IComparable

和 CompareTo 方法:

public int CompareTo(object other) {
    if(this.GetType() != other.GetType()) {
        return Comparer.Default.Compare(this, other);
    }
    return (this.Hours*60 + this.Minutes).CompareTo(
        ((ErrorAfterObject)other).Hours*60 + ((ErrorAfterObject)other).Minutes);
}

按预期工作。

于 2013-09-26T05:50:37.260 回答