加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 当T可以是IEnumerable时实现IEquatable

发布时间:2020-12-15 08:16:27 所属栏目:百科 来源:网络整理
导读:我已经阅读了与我类似的各种问题,但它们似乎都没有解决我的问题. 我是这样的类型: class MyObjectT : IEquatableMyObjectT { // no generic constraints private readonly string otherProp; private readonly T value; public MyObject(string otherProp,T
我已经阅读了与我类似的各种问题,但它们似乎都没有解决我的问题.

我是这样的类型:

class MyObject<T> : IEquatable<MyObject<T>> { // no generic constraints
  private readonly string otherProp;
  private readonly T value;

  public MyObject(string otherProp,T value)
  {
    this.otherProp = otherProp;
    this.value = value;
  }

  public string OtherProp { get { return this.otherProp; } }

  public T Value { get { return this.value; } }

  // ...

  public bool Equals(MyObject<T> other)
  {
    if (other == null)
    {
       return false;
    }

    return this.OtherProp.Equals(other.OtherProp) && this.Value.Equals(other.Value);
  }
}

当T是标量时,MyObject< int>平等工作正常,但当我定义的东西
喜欢MyObject< IEnumerable< int>>平等失败.

原因是当T是IEnumerable< T>时我应该调用this.Value.SequenceEqual(other.Value).

处理这种差异使得Equals(MyObject< T>)膨胀过于LOC类型检查和反射(对我来说,导致违反SOLID / SRP).

我无法在MSDN指南中找到这个具体案例,所以如果有人已经遇到过这个问题;如果可以分享这些知识,那就太棒了.

编辑:替代方案

对于K.I.S.S.,我想知道做类似的事情:

class MyObject<T> : IEquatable<MyObject<T>> {
  private readonly IEnumerable<T> value;

  // remainder omitted
}

通过这种方式,Equal的实现将非常简单.当我只需要一个值时,我就收集了1个项目.
显然T不是可枚举的(但数据结构是私有的,所以没有问题).

解决方法

你可以拥有你的MyObject< T>为你的类型T采用相等比较器并使用:
class MyObject<T> : IEquatable<MyObject<T>>
{
    private readonly IEqualityComparer<T> comparer;
    public MyObject(string otherProp,T value,IEqualityComparer<T> comparer)
    {
        this.comparer = comparer;
    }
    public MyObject(string otherProp,T value)
        : this(otherProp,value,EqualityComparer<T>.Default)
    {
    }

    public bool Equals(MyObject<T> other)
    {
        return OtherProp.Equals(other.OtherProp) && comparer.Equals(this.Value,other.Value);
    }
}

然后,对于IEnumerable< T>您可以使用比较器来比较序列而不是参考.您可能希望使用工厂方法来创建对象,以确保对同一T使用相同的比较器类型,以确保相等性保持对称.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读