TypeConverter.CanConvertTo Method

Definition

Returns whether this converter can convert the object to the specified type.

Overloads

CanConvertTo(Type)

Returns whether this converter can convert the object to the specified type.

CanConvertTo(ITypeDescriptorContext, Type)

Returns whether this converter can convert the object to the specified type, using the specified context.

CanConvertTo(Type)

Source:
TypeConverter.cs
Source:
TypeConverter.cs
Source:
TypeConverter.cs

Returns whether this converter can convert the object to the specified type.

C#
public bool CanConvertTo(Type destinationType);
C#
public bool CanConvertTo(Type? destinationType);

Parameters

destinationType
Type

A Type that represents the type you want to convert to.

Returns

true if this converter can perform the conversion; otherwise, false.

Remarks

If destinationType is a string, the default implementation of CanConvertTo always returns true.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0, 2.1

CanConvertTo(ITypeDescriptorContext, Type)

Source:
TypeConverter.cs
Source:
TypeConverter.cs
Source:
TypeConverter.cs

Returns whether this converter can convert the object to the specified type, using the specified context.

C#
public virtual bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, Type destinationType);
C#
public virtual bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, Type? destinationType);

Parameters

context
ITypeDescriptorContext

An ITypeDescriptorContext that provides a format context.

destinationType
Type

A Type that represents the type you want to convert to.

Returns

true if this converter can perform the conversion; otherwise, false.

Remarks

Use the context parameter to extract additional information about the environment from which this converter is invoked. This parameter can be null, so always check it. Also, properties on the context object can return null.

If destinationType is a string, the default implementation of CanConvertTo always returns true.

Notes to Inheritors

Override this method to provide your own conversion requirements.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0, 2.1