为什么 Equals 方法中会忽略 Uri 的片段?
我试图根据 URI 维护对象的集合:
public class ConceptCollection : KeyedCollection<Uri, Concept> {
protected override Uri GetKeyForItem(Concept item) {
return item.Uri;
}
}
但是,URI 通常仅根据 Uri 的片段而有所不同。因此,以下内容会导致错误:
ConceptCollection wines = new ConceptCollection();
Concept red = new Concept("http://www.w3.org/2002/07/owl#RedWine");
Concept white = new Concept("http://www.w3.org/2002/07/owl#WhiteWine");
wines.Add(red);
wines.Add(white); // Error: An item with the same key has already been added.
Per http://msdn.microsoft.com/ en-us/library/f83xtf15.aspx:
Equals方法比较两者 不考虑用户的实例 信息(UserInfo)和片段( 片段)他们可能的部分 包含。例如,给定 URI http://www.contoso.com/index.htm#search 和 http://user: [电子邮件受保护]/index.htm, Equals 方法将返回 true。
我已经接受了必须解决这个问题。但为什么它会这样呢?我可以看到用户信息的逻辑,但看不到片段的逻辑。
I'm trying to maintain a collection of objects based on their URI:
public class ConceptCollection : KeyedCollection<Uri, Concept> {
protected override Uri GetKeyForItem(Concept item) {
return item.Uri;
}
}
However, the URI regularly only differs based on the Fragment of the Uri. So, the following causes an error:
ConceptCollection wines = new ConceptCollection();
Concept red = new Concept("http://www.w3.org/2002/07/owl#RedWine");
Concept white = new Concept("http://www.w3.org/2002/07/owl#WhiteWine");
wines.Add(red);
wines.Add(white); // Error: An item with the same key has already been added.
Per http://msdn.microsoft.com/en-us/library/f83xtf15.aspx:
The Equals method compares the two
instances without regard to user
information ( UserInfo) and fragment (
Fragment) parts that they might
contain. For example, given the URIs
http://www.contoso.com/index.htm#search
and
http://user:[email protected]/index.htm,
the Equals method would return true.
I'm resigned to having to hack around this. But why does it behave this way? I can see the logic for user-info, but not for fragment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 RFC 2396:
添加的强调是我的,也是在 Uri.Equals 实现中不考虑该片段的原因。
在您的示例中,您要检索的资源的 URI 为: http://www.w3.org /2002/07/owl
片段由用户代理处理,对资源的实际检索没有任何意义或影响。
From RFC 2396:
The emphasis added is mine and is the reason the fragment is not considered in the Uri.Equals implementation.
In your example, the URI for the resource you are retrieving is: http://www.w3.org/2002/07/owl
The fragments are processed by the user agent and have no meaning to or influence on the actual retrieval of the resource.
我猜是因为除了片段之外的 2 个相同的 URI 仍然引用相同的资源,只是资源中的不同位置。
因此,如果您问“这些是相同的资源吗?”那么你可能会认为忽略该片段是正确的。
I guess because 2 URIs that are identical apart from the fragment still refer to the same resource, just a different location within the resource.
So if you're asking the question 'are these the same resource?' then you could argue that it's correct to ignore the fragment.