2012-02-11 98 views
1

我无法知道如何为泛型类实现IComparable接口方法CompareTo实现通用类的Ipaparable接口

我有一个名为BindingProperty<T>的类,它用于创建List<BindingProperty<intOrString>>以绑定到DataGrid。问题在于我无法执行排序操作,因为IComparable接口未由此BindingProperty<T>类实现。比较的结果将取决于BindingProperty<T>类的数据成员'值',其中'值'是类型T.当我单击DataGrid的列标题时,我得到一个例外CompareTo()方法未实现。

我需要帮助来实现这个接口。我应该使用IComparable<T>?如果是的话,我该怎么做?

在此先感谢 沙克蒂

+0

什么是泛型列表的类型? – twilson 2012-02-11 12:41:33

+0

IList已用于绑定DataGrid列。 – 2012-02-11 13:34:21

回答

0

建立一个泛型类型约束上T.

public class BindingProperty<T> : IComparable where T : IComparable 
{ 
    public T Value {get; set;} 

    public int CompareTo(object obj) 
    { 
     if (obj == null) return 1; 

     var other = obj as BindingProperty<T>; 
     if (other != null) 
      return Value.CompareTo(other.Value); 
     else 
      throw new ArgumentException("Object is not a BindingProperty<T>"); 
    } 
} 

编辑:
替代解决方案来处理,如果值不落实IComparable。这将支持所有类型,只是如果它们不执行IComparable就不会进行排序。

public class BindingProperty<T> : IComparable 
    { 
     public T Value {get; set;} 

     public int CompareTo(object obj) 
     { 
      if (obj == null) return 1; 

      var other = obj as BindingProperty<T>; 
      if (other != null) 
      { 
       var other2 = other as IComparable; 
       if(other2 != null) 
        return other2.CompareTo(Value); 
       else 
        return 1; //Does not implement IComparable, always return 1 
      } 
      else 
       throw new ArgumentException("Object is not a BindingProperty<T>"); 
     } 
    } 
+0

当我实现上述解决方案时,出现错误消息_“错误:类型'System.Windows.Media.Imaging.BitmapImage'不能用作泛型类型或方法'Bally.UI'中的类型参数'T'。 Framework.BindingProperty '。没有从'System.Windows.Media.Imaging.BitmapImage'到'System.IComparable '_的隐式引用转换。“这是来自项目的另一部分,BindingProperty被用作** BindingProperty **。是否有出路? – 2012-02-11 13:48:07

+1

泛型类型必须实现'IComparable'或者你不能对它进行排序'BitmapImage我不明白在图像上排序 – Magnus 2012-02-11 13:50:52

+0

我理解实现IComparable是必不可少的,但代码的另一部分(作为现有框架的一部分)确实具有此BitmapImage作为BindingProperty作为类型 – 2012-02-13 17:30:44

0

如果类型是一个自定义类,那么你的自定义类必须IComparable的。

例如

List<FooClass>,则FooClass需要继承/执行IComparable

+0

谢谢。但是所有的类T都不能实现IComparable,因为像BitmapImage,CustomCommands(实现UICommand并具有BindingProperty >成员)类是不兼容的。我不知道该怎么做。 – 2012-02-11 13:50:36

0

您可以使用通用约束来要求T实现IComparable<T>。然后您可以比较两个BindingProperty<T>实例,比较它们的Value成员。我不清楚您是否需要您的班级实施IComparableIComparable<T>,但实施这两项工作并不多。

class BindingProperty<T> 
    : IComparable<BindingProperty<T>>, IComparable where T : IComparable<T> { 

    public T Value { get; set; } 

    public Int32 CompareTo(BindingProperty<T> other) { 
    return Value.CompareTo(other.Value); 
    } 

    public Int32 CompareTo(Object other) { 
    if (other == null) 
     return 1; 
    var otherBindingProperty = other as BindingProperty<T>; 
    if (otherBindingProperty == null) 
     throw new ArgumentException(); 
    return CompareTo(otherBindingProperty); 
    } 

} 
+0

当我实现上述解决方案时,出现错误消息_“错误:类型'System.Windows.Media.Imaging.BitmapImage'不能用作泛型类型或方法'Bally.UI'中的类型参数'T'。 Framework.BindingProperty '。不存在从'System.Windows.Media.Imaging.BitmapImage'到'System.IComparable '_的隐式引用转换。这是来自项目的另一部分。 BindingProperty被用作** BindingProperty **。有出路吗? – 2012-02-11 13:43:31