简体   繁体   English

EqualityComparer <Uri> .Default.Equals()返回错误的结果还是什么?

[英]EqualityComparer<Uri>.Default.Equals() returning wrong result or what?

Is there an explanation for this other than being a bug in the .NET Framework? 除了作为.NET Framework中的错误之外,还有其他解释吗? The EqualityComparer<Uri>.Default.Equals() method is saying that the following URLs are equal! EqualityComparer<Uri>.Default.Equals()方法表示以下URL是相同的!

http://books.google.com/books?id=B84KAQAAIAAJ&pg=PA29&lpg=PA29&dq=fletcher+sandford+tilton&source=bl&ots=ou8eF5REOG&sig=74fzA11Z8AENBtyCUcXEsXV06jQ&hl=en&ei=2rHTS9LaN4249gTOh_GrDw&sa=X&oi=book_result&ct=result&resnum=3&ved=0CA0Q6AEwAg#v=onepage&q=fletcher s http://books.google.com/books?id=B84KAQAAIAAJ&pg=PA29&lpg=PA29&dq=fletcher+sandford+tilton&source=bl&ots=ou8eF5REOG&sig=74fzA11Z8AENBtyCUcXEsXV06jQ&hl=en&ei=2rHTS9LaN4249gTOh_GrDw&sa=X&oi=book_result&ct=result&resnum=3&ved=0CA0Q6AEwAg#v=onepage&q=弗莱彻

and

http://books.google.com/books?id=B84KAQAAIAAJ&pg=PA29&lpg=PA29&dq=fletcher+sandford+tilton&source=bl&ots=ou8eF5REOG&sig=74fzA11Z8AENBtyCUcXEsXV06jQ&hl=en&ei=2rHTS9LaN4249gTOh_GrDw&sa=X&oi=book_result&ct=result&resnum=3&ved=0CA0Q6AEwAg#v=onepage&q=fletcher http://books.google.com/books?id=B84KAQAAIAAJ&pg=PA29&lpg=PA29&dq=fletcher+sandford+tilton&source=bl&ots=ou8eF5REOG&sig=74fzA11Z8AENBtyCUcXEsXV06jQ&hl=en&ei=2rHTS9LaN4249gTOh_GrDw&sa=X&oi=book_result&ct=result&resnum=3&ved=0CA0Q6AEwAg#v=onepage&q=弗莱彻

Notice the space followed by 's' at the end of the first one. 注意第一个空格后面的空格后跟's'。

Well, the concern (whether right or wrong) isn't in EqualityComparer<Uri>.Default . 好吧,关注(无论是对还是错)不在EqualityComparer<Uri>.Default It calls into Uri.Equals() as it should. 它会调用Uri.Equals()

Now, Uri.Equals() ignores differences on fragment alone. 现在, Uri.Equals()忽略了片段上的差异。 In a great many cases that is appropriate. 在很多情况下这是恰当的。 In a great many it's not. 在许多人中并非如此。 Personally I wouldn't have had it as the default, but then since I'm not someone who coded it perhaps I don't know of some compelling reasons to have things as they are. 就个人而言,我不会将它作为默认设置,但是因为我不是编码它的人,或许我不知道有什么令人信服的理由来保持原样。

Note that this is documented. 请注意,这是记录在案的。

Other decisions are also debatable (that it ignores case-difference on host components matches many practical concerns about URIs, but not how URI equality is defined in some specifications). 其他决策也是值得商榷的(它忽略了主机组件上的大小写差异与许多关于URI的实际问题相匹配,而不是在某些规范中如何定义URI相等性)。

If you need a stricter equality than that, I suggest you define your own comparator: 如果你需要更严格的平等,我建议你定义自己的比较器:

public class UriStictEqualityComparer : IEqualityComparer<Uri>
{
  public bool Equals(Uri x, Uri y)
  {
    return ReferenceEquals(x, y)
      ||
      (
        x != null
        &&
        y != null
        &&
        x.IsAbsoluteUri == y.IsAbsoluteUri
        &&
        x.ToString() == y.ToString()
      );
  }
  public int GetHashCode(Uri obj)
  {
    return obj == null ? 0 : obj.ToString().GetHashCode();
  }
}

Still, you may find that you want some cases the above considers unequal, to be equal too. 尽管如此,你可能会发现你想要一些上述认为不平等的情况,也是相同的。 Eg you need to consider whether punycode and non-punycode versions are the same, whether escaped non-special characters should be unescaped, and so on. 例如,你需要考虑punycode和非punycode版本是否相同,是否应该转义转义的非特殊字符,等等。 Uri's Compare method can be a benefit in such cases. 在这种情况下,Uri的Compare方法可能是有益的。

The behavior you are seeing is by-design. 您看到的行为是按设计进行的。 Uri fragments are ignored in its Equals implementation because they are not technically part of the URI itself. Uri片段在其Equals实现中被忽略,因为它们在技术上不是URI本身的一部分。 The Fragment part of your Uri is "#v=onepage&q=fletcher" (the # symbol and everything following it). UriFragment部分是“#v = onepage&q = fletcher”(#符号及其后的所有内容)。

You can use a Uri 's Compare method and specify which UriComponents to include in the comparison. 您可以使用UriCompare方法并指定要包含在比较中的UriComponents

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM