为泛型类实现 Icomparable 接口

发布于 2025-01-04 14:25:51 字数 563 浏览 2 评论 0 原文

我无法找出如何为泛型类实现 IComparable 接口方法 CompareTo

我有一个名为 BindingProperty 的类,用于创建 List> 来绑定到 DataGrid。问题是我无法执行排序操作,因为此 BindingProperty 类未实现 IComparable 接口。比较的结果取决于 BindingProperty 类的数据成员“Value”,其中“Value”的类型为 T。当我单击 DataGrid 的列标题时,我收到一个异常,即 CompareTo() 方法未实现。

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

提前致谢 沙克蒂

I am not able to find out as to how to implement the IComparable interface method CompareTo for a generic class.

I have a class called BindingProperty<T> which is used to create a List<BindingProperty<intOrString>> to bind to a DataGrid. The problem is that the I can not perform the Sort operation as the IComparable interface is not implemented by this BindingProperty<T> class. The result of comparison would depend on a data member 'Value' of the the BindingProperty<T> class where 'Value' is of Type T. When I click on the column header of the DataGrid, I get an exception that CompareTo() method is not implemented.

I would need help to implement this interface. Should I be using IComparable<T>? If yes, how do I do it?

Thanks in advance
Shakti

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

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

发布评论

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

评论(3

过潦 2025-01-11 14:25:51

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

编辑:
如果 value 未实现 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>");
        }
    }

Set up a generic type constraint on 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>");
    }
}

Edit:
Alternative solution to handle if value does not implement IComparable. This will support all types, just wont sort if they do not implement 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>");
        }
    }
夏至、离别 2025-01-11 14:25:51

如果 Type 是自定义类,那么您的自定义类需要是 IComparable。

例如

List,则FooClass需要继承/实现IComparable

If the Type is a custom class, then your custom class needs to be IComparable.

e.g.

List<FooClass>, then FooClass needs to inherit/implement IComparable.

梦幻的心爱 2025-01-11 14:25:51

您可以使用通用约束来要求 T 实现 IComparable。然后,您可以通过比较两个 BindingProperty 实例的 Value 成员来比较它们。我不清楚您是否需要您的类实现 IComparableIComparable 但实现两者并不需要太多额外的工作。

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

}

You can use a generic constraint to require that T implements IComparable<T>. You can then compare two BindingProperty<T> instance by comparing their Value members. It is not clear to me if you need your class to implement IComparable or IComparable<T> but implemeting both is not much extra work.

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文