QueryStringConverter 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
此类将查询字符串中的参数转换为相应类型的对象。 它还可以将参数从对象转换为其查询字符串表示形式。
public ref class QueryStringConverter
public class QueryStringConverter
type QueryStringConverter = class
Public Class QueryStringConverter
- 继承
-
QueryStringConverter
- 派生
示例
以下代码演示如何使用 QueryStringConverter 类在字符串和 32 位整数之间进行转换。
QueryStringConverter converter = new QueryStringConverter();
if (converter.CanConvert(typeof(Int32)))
converter.ConvertStringToValue("123", typeof(Int32));
int value = 321;
string strValue = converter.ConvertValueToString(value, typeof(Int32));
Console.WriteLine("the value = {0}, the string representation of the value = {1}", value, strValue);
Dim converter As New QueryStringConverter()
If (converter.CanConvert(GetType(Int32))) Then
converter.ConvertStringToValue("123", GetType(Int32))
End If
Dim value As Integer = 321
Dim strValue As String = converter.ConvertValueToString(value, GetType(Int32))
Console.WriteLine("the value = {0}, the string representation of the value = {1}", value, strValue)
注解
可以在 URL 中的查询字符串中指定参数。 此类采用字符串中指定的这些参数,并将其转换为对象。 例如,定义了以下协定。
[ServiceContract]
interface Calculator
{
[WebGet(UriTemplate="Add?n1={n1}&n2={n2}")]
[OperationContract]
long Add(long n1, long n2);
}
Windows Communication Foundation (WCF) 服务实现此接口,并在WebHttpBehaviorhttp://localhost:8000/MyCalcService的终结点上公开该接口。
Add可以通过将 HTTP GET 发送到http://localhost:8000/MyCalcService/Add?n1=10&n2=5来调用服务操作。 接收 QueryStringConverter 此 URL,并将 URL 中指定的两个参数(n1 和 n2)转换为具有相应值的两个 long 对象。
可以从派生类 QueryStringConverter 来控制查询字符串参数如何映射到服务操作的参数。
默认情况下,支持 QueryStringConverter 以下类型:
Byte 数组
Enums
具有可在类型和字符串表示形式之间来回进行转换的
TypeConverterAttribute的类型。
构造函数
| 名称 | 说明 |
|---|---|
| QueryStringConverter() |
初始化 QueryStringConverter 类的新实例。 |
方法
| 名称 | 说明 |
|---|---|
| CanConvert(Type) |
确定指定类型是否可以转换为字符串表示形式和从字符串表示形式转换。 |
| ConvertStringToValue(String, Type) |
将查询字符串参数转换为指定类型。 |
| ConvertValueToString(Object, Type) |
将参数转换为查询字符串表示形式。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |