TypeConverter.ConvertTo 方法

定义

将给定值对象转换为指定的类型。Converts the given value object to the specified type.

重载

ConvertTo(Object, Type)

使用参数将给定的值对象转换为指定的类型。Converts the given value object to the specified type, using the arguments.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

使用指定的上下文和区域性信息将给定值对象转换为指定的类型。Converts the given value object to the specified type, using the specified context and culture information.

ConvertTo(Object, Type)

使用参数将给定的值对象转换为指定的类型。Converts the given value object to the specified type, using the arguments.

public:
 System::Object ^ ConvertTo(System::Object ^ value, Type ^ destinationType);
public object ConvertTo (object value, Type destinationType);
member this.ConvertTo : obj * Type -> obj
Public Function ConvertTo (value As Object, destinationType As Type) As Object

参数

value
Object

要转换的 ObjectThe Object to convert.

destinationType
Type

value 参数要转换成的 TypeThe Type to convert the value parameter to.

返回

一个 Object,它表示转换后的值。An Object that represents the converted value.

异常

destinationType 参数为 nullThe destinationType parameter is null.

无法执行转换。The conversion cannot be performed.

注解

转换为和 from 的最常见类型是字符串对象。The most common type to convert to and from is a string object. 如果对象有效ToString并且目标类型是字符串, 则此实现将对对象调用。This implementation calls ToString on the object, if the object is valid and if the destination type is a string.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

使用指定的上下文和区域性信息将给定值对象转换为指定的类型。Converts the given value object to the specified type, using the specified context and culture information.

public:
 virtual System::Object ^ ConvertTo(System::ComponentModel::ITypeDescriptorContext ^ context, System::Globalization::CultureInfo ^ culture, System::Object ^ value, Type ^ destinationType);
public virtual object ConvertTo (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType);
abstract member ConvertTo : System.ComponentModel.ITypeDescriptorContext * System.Globalization.CultureInfo * obj * Type -> obj
override this.ConvertTo : System.ComponentModel.ITypeDescriptorContext * System.Globalization.CultureInfo * obj * Type -> obj
Public Overridable Function ConvertTo (context As ITypeDescriptorContext, culture As CultureInfo, value As Object, destinationType As Type) As Object

参数

context
ITypeDescriptorContext

一个 ITypeDescriptorContext,用于提供格式上下文。An ITypeDescriptorContext that provides a format context.

culture
CultureInfo

CultureInfoA CultureInfo. 如果传递 null,则采用当前区域性。If null is passed, the current culture is assumed.

value
Object

要转换的 ObjectThe Object to convert.

destinationType
Type

value 参数要转换成的 TypeThe Type to convert the value parameter to.

返回

一个 Object,它表示转换后的值。An Object that represents the converted value.

异常

destinationType 参数为 nullThe destinationType parameter is null.

无法执行转换。The conversion cannot be performed.

注解

最常见的转换类型是与字符串对象的相互转换。The most common types to convert are to and from a string object. 如果对象有效ToString并且目标类型是字符串, 则此实现将对对象调用。This implementation calls ToString on the object if the object is valid and if the destination type is a string.

使用 context 参数提取有关调用此转换器的环境的附加信息。Use the context parameter to extract additional information about the environment from which this converter is invoked. 此参数可以是null, 因此请始终选中此参数。This parameter can be null, so always check it. 同样,上下文对象的属性可以返回 nullAlso, properties on the context object can return null.

要支持 XAML 的类型转换行为的自定义类型作者通常实现一个TypeConverter类, 该类支持ConvertFrom字符串中的行为。Custom type authors that intend to support a type conversion behavior for XAML typically implement a TypeConverter class that supports ConvertFrom behavior from a string. 这些类型转换器还可以实现ConvertTo以支持序列化回 XAML。These type converters might also implement ConvertTo to support serialization back to XAML.

某些 XAML 序列化方案无法TypeConverter单独进行适当地寻址。Some XAML serialization scenarios cannot be adequately addressed with TypeConverter alone. 在这些情况下, 可能还需要定义一个ValueSerializer来处理序列化事例 (否则将ConvertTo处理此类序列化案例)。In these cases, it may be necessary to also define a ValueSerializer that handles the serialization cases that would otherwise be handled by ConvertTo. 有关详细信息,请参阅 ValueSerializerFor more information, see ValueSerializer.

继承者说明

重写该方法以提供您自己的转换需求。Override this method to provide your own conversion requirements.

有关用于支持 XAML 和自定义类型的类型转换器的实现模式, 请参阅XAML 的类型转换器概述For implementation patterns for type converters that are used to support XAML and custom types, see Type Converters for XAML Overview.

另请参阅

适用于