System.UriTemplateTable 类
方法描述
一个表示一组关联 UriTemplate 对象的类。
语法定义(C# System.UriTemplateTable 类 的用法)
public class UriTemplateTable
构造函数
| 构造函数名称 | 构造函数描述 | 
|---|---|
| UriTemplateTable() | 初始化 UriTemplateTable 类的新实例。 | 
| UriTemplateTable(Uri) | 使用指定的基址初始化 UriTemplateTable 类的新实例。 | 
| UriTemplateTable(IEnumerable | 使用指定的键/值对集合初始化 UriTemplateTable 类的新实例。 | 
| UriTemplateTable(Uri, IEnumerable | 使用指定的基址和键/值对集合初始化 UriTemplateTable 类的新实例。 | 
成员/方法
| 方法名称 | 方法描述 | 
|---|---|
| Equals(Object) | 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。) | 
| Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。) | 
| GetHashCode | 用作特定类型的哈希函数。 (继承自 Object。) | 
| GetType | 获取当前实例的 Type。 (继承自 Object。) | 
| MakeReadOnly | 将 UriTemplateTable 设置为只读。 | 
| Match | 尝试将候选 Uri 与 UriTemplateTable 匹配。 | 
| MatchSingle | 尝试将候选 Uri 与 UriTemplateTable 匹配。 | 
| MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) | 
| ToString | 返回表示当前对象的字符串。 (继承自 Object。) | 
提示和注释
UriTemplateTable 是一组绑定到开发人员所选对象的关联 UriTemplate 对象。您可以通过它将候选统一资源标识符 (URI) 与集合中的模板进行匹配,然后检索与匹配的模板相关联的数据。在调用 MakeReadOnly(Boolean) 方法之前,可以更改 UriTemplateTable 的内容,调用时会发生下列其中一种类型的验证:
如果在调用 MakeReadOnly(Boolean) 时传入 false,则 UriTemplateTable 会检查以确保表中没有多个结构等效的模板。如果找到这样的模板,则会引发异常。如果想要确保只有一个模板与传入的 URI 匹配,则可将此验证类型与 MatchSingle(Uri) 结合使用。
如果在调用 MakeReadOnly(Boolean) 时传入 true,则 UriTemplateTable 中可能包含多个结构等效的模板。但是,模板中的所有查询字符串必须无歧义;您可以使用相同的查询字符串。有关以下内容的更多信息有歧义查询字符串的更多信息,请参见 UriTemplate and UriTemplateTable。
System.UriTemplateTable 类例子
下面的代码演示如何创建 UriTemplateTable,对其进行填充,并使用它与候选 Uri 进行匹配。
Uri prefix = new Uri("http://localhost/");
//Create a series of templates
UriTemplate weatherByCity  = new UriTemplate("weather/ state}/ city}");
UriTemplate weatherByCountry = new UriTemplate("weather/ country}/ village}");       
UriTemplate weatherByState = new UriTemplate("weather/ state}");
UriTemplate traffic = new UriTemplate("traffic/*");
UriTemplate wildcard = new UriTemplate("*");
//Create a template table
UriTemplateTable table = new UriTemplateTable(prefix);
//Add each template to the table with some associated data
table.KeyValuePairs.Add(new KeyValuePair(weatherByCity, "weatherByCity"));
table.KeyValuePairs.Add(new KeyValuePair(weatherByCountry, "weatherByCountry"));
table.KeyValuePairs.Add(new KeyValuePair(weatherByState, "weatherByState"));
table.KeyValuePairs.Add(new KeyValuePair(traffic, "traffic"));
table.MakeReadOnly(true);
Console.WriteLine("KeyValuePairs:");
foreach (KeyValuePair keyPair in table.KeyValuePairs)
{
    Console.WriteLine("     0},  1}", keyPair.Key, keyPair.Value);
}
Console.WriteLine();
//Call MatchSingle to retrieve some match results:
ICollection results = null;
Uri weatherInSeattle = new Uri("http://localhost/weather/Washington/Seattle");
results = table.Match(weatherInSeattle);
if( results != null)
{
    Console.WriteLine("Matching templates:");
    foreach (UriTemplateMatch match in results)
    {
        Console.WriteLine("    0}", match.Template);
    }
}
      
线程安全
此类型的任何公共 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 系统要求。