gpt4 book ai didi

c# - 为什么在 Equals 方法中忽略了 Uri 的片段?

转载 作者:太空狗 更新时间:2023-10-29 22:35:17 27 4
gpt4 key购买 nike

我正在尝试根据对象的 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.

根据 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:password@www.contoso.com/index.htm, the Equals method would return true.

我已经辞职了,不得不绕过这个。但为什么它会这样呢?我可以看到用户信息的逻辑,但看不到片段的逻辑。

最佳答案

来自 RFC 2396 :

4.1. Fragment Identifier

When a URI reference is used to perform a retrieval action on the identified resource, the optional fragment identifier, separated from the URI by a crosshatch ("#") character, consists of additional reference information to be interpreted by the user agent after the retrieval action has been successfully completed. As such, it is not part of a URI, but is often used in conjunction with a URI.

添加的重点是我的,这也是 Uri.Equals 实现中未考虑该片段的原因。

在您的示例中,您正在检索的资源的 URI 是:http://www.w3.org/2002/07/owl

片段由用户代理处理,对资源的实际检索没有任何意义或影响。

关于c# - 为什么在 Equals 方法中忽略了 Uri 的片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1387255/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com