【发布时间】:2009-09-07 01:54:18
【问题描述】:
我正在尝试根据它们的 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:
Equals 方法比较两者 不考虑用户的实例 信息(UserInfo)和片段( 片段)部分,他们可能 包含。例如,给定 URI http://www.contoso.com/index.htm#search 和 http://user:password@www.contoso.com/index.htm, Equals 方法将返回 true。
我已经接受了不得不解决这个问题。但是为什么它会这样呢?我可以看到用户信息的逻辑,但不能看到片段。
【问题讨论】:
-
我使用过的任何 RDF 框架都必须实现自己的 Uri 类,因为 .NET System.Uri 实现不保持原始 Uri 的纯度。您可以在较小的项目中使用 System.Uri.OriginalString 来避免这种情况......不过,类似的问题会一遍又一遍地出现。