System.UriTemplateEquivalenceComparer 类
方法描述
一个用于比较 UriTemplate 实例以确定结构(而非引用)等效性的类。
语法定义(C# System.UriTemplateEquivalenceComparer 类 的用法)
public class UriTemplateEquivalenceComparer : IEqualityComparer
构造函数
| 构造函数名称 | 构造函数描述 |
|---|---|
| UriTemplateEquivalenceComparer | 初始化 UriTemplateEquivalenceComparer 类的新实例。 |
成员/方法
| 方法名称 | 方法描述 |
|---|---|
| Equals(Object) | 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。) |
| Equals(UriTemplate, UriTemplate) | 比较两个 UriTemplate 实例是否等效。 |
| Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。) |
| GetHashCode() | 用作特定类型的哈希函数。 (继承自 Object。) |
| GetHashCode(UriTemplate) | 获取所指定 UriTemplate 实例的哈希代码。 |
| GetType | 获取当前实例的 Type。 (继承自 Object。) |
| MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) |
| ToString | 返回表示当前对象的字符串。 (继承自 Object。) |
提示和注释
默认情况下,使用引用相等性来比较 UriTemplate 实例。 UriTemplateEquivalenceComparer 类会使用 IsEquivalentTo(UriTemplate) 而非默认的引用相等性检查来评估等效性。如果希望根据与两个 UriTemplate 实例匹配的 URI 而非严格的引用相等性来比较这两个实例,则此类会非常有用。
System.UriTemplateEquivalenceComparer 类例子
同时也说明了在字典中使用 UriTemplateEquivalenceComparer 作为比较器的方法。
// Define two structurally equivalent templates
UriTemplate temp1 = new UriTemplate("weather/{state}/{city}");
UriTemplate temp2 = new UriTemplate("weather/{country}/{village}");
// Notice they are not reference equal, in other words
// they are do not refer to the same object
if (temp1 == temp2)
Console.WriteLine("{0} and {1} are reference equal", temp1, temp2);
else
Console.WriteLine("{0} and {1} are NOT reference equal", temp1, temp2);
// Notice they are structrually equal
if (temp1.IsEquivalentTo(temp2))
Console.WriteLine("{0} and {1} are structurally equal", temp1, temp2);
else
Console.WriteLine("{0} and {1} are NOT structurally equal", temp1, temp2);
// Create a dictionary and use UriTemplateEquivalenceComparer as the comparer
Dictionary templates = new Dictionary(new UriTemplateEquivalenceComparer());
// Add template 1 into the dictionary
templates.Add(temp1, "template1");
// The UriTemplateEquivalenceComparer will be used here to compare the template in the table with template2
// they are structurally equivalent, so ContainsKey will return true.
if (templates.ContainsKey(temp2))
Console.WriteLine("Both templates hash to the same value");
else
Console.WriteLine("Both templates do NOT hash to the same value");
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
版本信息
.NET Framework 受以下版本支持:4、3.5 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。