TypeConverter.ConvertTo TypeConverter.ConvertTo TypeConverter.ConvertTo TypeConverter.ConvertTo Method

定義

會將指定值物件轉換成指定型別。Converts the given value object to the specified type.

多載

ConvertTo(Object, Type) ConvertTo(Object, Type) ConvertTo(Object, Type) ConvertTo(Object, Type)

會使用引數,將指定值物件轉換成指定型別。Converts the given value object to the specified type, using the arguments.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

使用指定的內容和文化特性資訊,將指定值物件轉換成指定類型。Converts the given value object to the specified type, using the specified context and culture information.

ConvertTo(Object, Type) ConvertTo(Object, Type) ConvertTo(Object, Type) 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 Object Object Object

要進行轉換的 ObjectThe Object to convert.

destinationType
Type Type Type 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 的最常見類型是 string 物件。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) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) 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

參數

culture
CultureInfo CultureInfo CultureInfo CultureInfo

CultureInfoA CultureInfo. 如果傳遞 null ,則會採用目前的文化特性。If null is passed, the current culture is assumed.

value
Object Object Object Object

要進行轉換的 ObjectThe Object to convert.

destinationType
Type Type Type 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.

另請參閱

適用於