TypeConverter Class
TOC
Collapse the table of content
Expand the table of content

TypeConverter Class

 

Provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.

Namespace:   System.ComponentModel
Assembly:  System (in System.dll)

System.Object
  System.ComponentModel.TypeConverter
    Microsoft.VisualBasic.Activities.XamlIntegration.VisualBasicSettingsConverter
    Microsoft.VisualBasic.ApplicationServices.BuiltInRoleConverter
    System.Activities.XamlIntegration.AssemblyReferenceConverter
    System.Activities.XamlIntegration.DynamicUpdateMapConverter
    System.Activities.XamlIntegration.DynamicUpdateMapItemConverter
    System.Activities.XamlIntegration.ImplementationVersionConverter
    System.Activities.XamlIntegration.TypeConverterBase
    System.Activities.XamlIntegration.WorkflowIdentityConverter
    System.ComponentModel.BaseNumberConverter
    System.ComponentModel.BooleanConverter
    System.ComponentModel.CharConverter
    System.ComponentModel.CollectionConverter
    System.ComponentModel.CultureInfoConverter
    System.ComponentModel.DateTimeConverter
    System.ComponentModel.DateTimeOffsetConverter
    System.ComponentModel.EnumConverter
    System.ComponentModel.ExpandableObjectConverter
    System.ComponentModel.GuidConverter
    System.ComponentModel.MultilineStringConverter
    System.ComponentModel.NullableConverter
    System.ComponentModel.ReferenceConverter
    System.ComponentModel.StringConverter
    System.ComponentModel.TimeSpanConverter
    System.ComponentModel.TypeListConverter
    System.Configuration.ConfigurationConverterBase
    System.Diagnostics.Design.LogConverter
    System.Drawing.ColorConverter
    System.Drawing.FontConverter
    System.Drawing.FontConverter.FontNameConverter
    System.Drawing.ImageConverter
    System.Drawing.ImageFormatConverter
    System.Drawing.PointConverter
    System.Drawing.RectangleConverter
    System.Drawing.SizeConverter
    System.Drawing.SizeFConverter
    System.Resources.ResXFileRef.Converter
    System.Security.Authentication.ExtendedProtection.ExtendedProtectionPolicyTypeConverter
    System.ServiceModel.Discovery.Configuration.DiscoveryVersionConverter
    System.ServiceModel.XamlIntegration.EndpointIdentityConverter
    System.ServiceModel.XamlIntegration.ServiceXNameTypeConverter
    System.ServiceModel.XamlIntegration.XPathMessageContextTypeConverter
    System.UriTypeConverter
    System.Web.Configuration.LowerCaseStringConverter
    System.Web.UI.Design.DataBindingCollectionConverter
    System.Web.UI.Design.DataColumnSelectionConverter
    System.Web.UI.Design.DataFieldConverter
    System.Web.UI.Design.DataMemberConverter
    System.Web.UI.Design.DataSourceConverter
    System.Web.UI.Design.DataSourceViewSchemaConverter
    System.Web.UI.Design.ExpressionsCollectionConverter
    System.Web.UI.Design.MobileControls.Converters.DataFieldConverter
    System.Web.UI.Design.MobileControls.Converters.DataMemberConverter
    System.Web.UI.Design.SkinIDTypeConverter
    System.Web.UI.Design.WebControls.DataSourceIDConverter
    System.Web.UI.WebControls.FontNamesConverter
    System.Web.UI.WebControls.FontUnitConverter
    System.Web.UI.WebControls.StringArrayConverter
    System.Web.UI.WebControls.UnitConverter
    System.Windows.Controls.DataGridLengthConverter
    System.Windows.Controls.Ribbon.RibbonControlLengthConverter
    System.Windows.Controls.Ribbon.StringCollectionConverter
    System.Windows.Controls.VirtualizationCacheLengthConverter
    System.Windows.CornerRadiusConverter
    System.Windows.CultureInfoIetfLanguageTagConverter
    System.Windows.DeferrableContentConverter
    System.Windows.DialogResultConverter
    System.Windows.DurationConverter
    System.Windows.DynamicResourceExtensionConverter
    System.Windows.ExpressionConverter
    System.Windows.FigureLengthConverter
    System.Windows.FontSizeConverter
    System.Windows.FontStretchConverter
    System.Windows.FontStyleConverter
    System.Windows.FontWeightConverter
    System.Windows.Forms.AxHost.StateConverter
    System.Windows.Forms.CursorConverter
    System.Windows.Forms.DataGridPreferredColumnWidthTypeConverter
    System.Windows.Forms.DataGridViewCellStyleConverter
    System.Windows.Forms.KeysConverter
    System.Windows.Forms.Layout.TableLayoutSettingsTypeConverter
    System.Windows.Forms.LinkArea.LinkAreaConverter
    System.Windows.Forms.LinkConverter
    System.Windows.Forms.ListBindingConverter
    System.Windows.Forms.OpacityConverter
    System.Windows.Forms.PaddingConverter
    System.Windows.Forms.ScrollableControl.DockPaddingEdgesConverter
    System.Windows.Forms.SelectionRangeConverter
    System.Windows.Forms.TreeNodeConverter
    System.Windows.GridLengthConverter
    System.Windows.Input.CommandConverter
    System.Windows.Input.CursorConverter
    System.Windows.Input.InputScopeConverter
    System.Windows.Input.InputScopeNameConverter
    System.Windows.Input.KeyConverter
    System.Windows.Input.KeyGestureConverter
    System.Windows.Input.ModifierKeysConverter
    System.Windows.Input.MouseActionConverter
    System.Windows.Input.MouseGestureConverter
    System.Windows.Int32RectConverter
    System.Windows.KeySplineConverter
    System.Windows.KeyTimeConverter
    System.Windows.LengthConverter
    System.Windows.Markup.DependencyPropertyConverter
    System.Windows.Markup.EventSetterHandlerConverter
    System.Windows.Markup.NameReferenceConverter
    System.Windows.Markup.RoutedEventConverter
    System.Windows.Markup.SetterTriggerConditionValueConverter
    System.Windows.Markup.TemplateKeyConverter
    System.Windows.Markup.XmlLanguageConverter
    System.Windows.Media.Animation.RepeatBehaviorConverter
    System.Windows.Media.BrushConverter
    System.Windows.Media.CacheModeConverter
    System.Windows.Media.ColorConverter
    System.Windows.Media.Converters.BaseIListConverter
    System.Windows.Media.DoubleCollectionConverter
    System.Windows.Media.FontFamilyConverter
    System.Windows.Media.GeometryConverter
    System.Windows.Media.ImageSourceConverter
    System.Windows.Media.Int32CollectionConverter
    System.Windows.Media.MatrixConverter
    System.Windows.Media.Media3D.Matrix3DConverter
    System.Windows.Media.Media3D.Point3DCollectionConverter
    System.Windows.Media.Media3D.Point3DConverter
    System.Windows.Media.Media3D.Point4DConverter
    System.Windows.Media.Media3D.QuaternionConverter
    System.Windows.Media.Media3D.Rect3DConverter
    System.Windows.Media.Media3D.Size3DConverter
    System.Windows.Media.Media3D.Vector3DCollectionConverter
    System.Windows.Media.Media3D.Vector3DConverter
    System.Windows.Media.PathFigureCollectionConverter
    System.Windows.Media.PixelFormatConverter
    System.Windows.Media.PointCollectionConverter
    System.Windows.Media.RequestCachePolicyConverter
    System.Windows.Media.TransformConverter
    System.Windows.Media.VectorCollectionConverter
    System.Windows.PointConverter
    System.Windows.PropertyPathConverter
    System.Windows.RectConverter
    System.Windows.SizeConverter
    System.Windows.StrokeCollectionConverter
    System.Windows.TemplateBindingExpressionConverter
    System.Windows.TemplateBindingExtensionConverter
    System.Windows.TextDecorationCollectionConverter
    System.Windows.ThicknessConverter
    System.Windows.VectorConverter
    System.Workflow.ComponentModel.Design.ActivityBindTypeConverter
    System.Xaml.Schema.XamlTypeTypeConverter

[ComVisibleAttribute(true)]
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)]
public class TypeConverter

NameDescription
System_CAPS_pubmethodTypeConverter()

Initializes a new instance of the TypeConverter class.

NameDescription
System_CAPS_pubmethodCanConvertFrom(ITypeDescriptorContext, Type)

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

System_CAPS_pubmethodCanConvertFrom(Type)

Returns whether this converter can convert an object of the given type to the type of this converter.

System_CAPS_pubmethodCanConvertTo(ITypeDescriptorContext, Type)

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

System_CAPS_pubmethodCanConvertTo(Type)

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

System_CAPS_pubmethodConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

Converts the given object to the type of this converter, using the specified context and culture information.

System_CAPS_pubmethodConvertFrom(Object)

Converts the given value to the type of this converter.

System_CAPS_pubmethodConvertFromInvariantString(ITypeDescriptorContext, String)

Converts the given string to the type of this converter, using the invariant culture and the specified context.

System_CAPS_pubmethodConvertFromInvariantString(String)

Converts the given string to the type of this converter, using the invariant culture.

System_CAPS_pubmethodConvertFromString(ITypeDescriptorContext, CultureInfo, String)

Converts the given text to an object, using the specified context and culture information.

System_CAPS_pubmethodConvertFromString(ITypeDescriptorContext, String)

Converts the given text to an object, using the specified context.

System_CAPS_pubmethodConvertFromString(String)

Converts the specified text to an object.

System_CAPS_pubmethodConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

Converts the given value object to the specified type, using the specified context and culture information.

System_CAPS_pubmethodConvertTo(Object, Type)

Converts the given value object to the specified type, using the arguments.

System_CAPS_pubmethodConvertToInvariantString(ITypeDescriptorContext, Object)

Converts the specified value to a culture-invariant string representation, using the specified context.

System_CAPS_pubmethodConvertToInvariantString(Object)

Converts the specified value to a culture-invariant string representation.

System_CAPS_pubmethodConvertToString(ITypeDescriptorContext, CultureInfo, Object)

Converts the given value to a string representation, using the specified context and culture information.

System_CAPS_pubmethodConvertToString(ITypeDescriptorContext, Object)

Converts the given value to a string representation, using the given context.

System_CAPS_pubmethodConvertToString(Object)

Converts the specified value to a string representation.

System_CAPS_pubmethodCreateInstance(IDictionary)

Re-creates an Object given a set of property values for the object.

System_CAPS_pubmethodCreateInstance(ITypeDescriptorContext, IDictionary)

Creates an instance of the type that this TypeConverter is associated with, using the specified context, given a set of property values for the object.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_protmethodGetConvertFromException(Object)

Returns an exception to throw when a conversion cannot be performed.

System_CAPS_protmethodGetConvertToException(Object, Type)

Returns an exception to throw when a conversion cannot be performed.

System_CAPS_pubmethodGetCreateInstanceSupported()

Returns whether changing a value on this object requires a call to the CreateInstance method to create a new value.

System_CAPS_pubmethodGetCreateInstanceSupported(ITypeDescriptorContext)

Returns whether changing a value on this object requires a call to CreateInstance to create a new value, using the specified context.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetProperties(ITypeDescriptorContext, Object)

Returns a collection of properties for the type of array specified by the value parameter, using the specified context.

System_CAPS_pubmethodGetProperties(ITypeDescriptorContext, Object, Attribute[])

Returns a collection of properties for the type of array specified by the value parameter, using the specified context and attributes.

System_CAPS_pubmethodGetProperties(Object)

Returns a collection of properties for the type of array specified by the value parameter.

System_CAPS_pubmethodGetPropertiesSupported()

Returns whether this object supports properties.

System_CAPS_pubmethodGetPropertiesSupported(ITypeDescriptorContext)

Returns whether this object supports properties, using the specified context.

System_CAPS_pubmethodGetStandardValues()

Returns a collection of standard values from the default context for the data type this type converter is designed for.

System_CAPS_pubmethodGetStandardValues(ITypeDescriptorContext)

Returns a collection of standard values for the data type this type converter is designed for when provided with a format context.

System_CAPS_pubmethodGetStandardValuesExclusive()

Returns whether the collection of standard values returned from GetStandardValues is an exclusive list.

System_CAPS_pubmethodGetStandardValuesExclusive(ITypeDescriptorContext)

Returns whether the collection of standard values returned from GetStandardValues is an exclusive list of possible values, using the specified context.

System_CAPS_pubmethodGetStandardValuesSupported()

Returns whether this object supports a standard set of values that can be picked from a list.

System_CAPS_pubmethodGetStandardValuesSupported(ITypeDescriptorContext)

Returns whether this object supports a standard set of values that can be picked from a list, using the specified context.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodIsValid(ITypeDescriptorContext, Object)

Returns whether the given value object is valid for this type and for the specified context.

System_CAPS_pubmethodIsValid(Object)

Returns whether the given value object is valid for this type.

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodSortProperties(PropertyDescriptorCollection, String[])

Sorts a collection of properties.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

The most common type of converter is one that converts to and from a text representation. The type converter for a class is bound to the class with a TypeConverterAttribute. Unless this attribute is overridden, all classes that inherit from this class use the same type converter as the base class.

System_CAPS_noteNote

For general type system purposes, do not access a type converter directly. Instead, access the appropriate converter by using TypeDescriptor. For more information, see the code examples provided.

Note   For XAML purposes, a XAML processor searches for the TypeConverterAttribute directly, instead of going through TypeDescriptor. For cases where you do want a TypeDescriptor instance from code, or where you create a shared instance in WPF resources, it is acceptable to construct it directly without referencing TypeDescriptor or other reflection and type system support.

Classes derived from TypeConverter are often referenced as part of how a XAML processor converts an attribute or initialization text value from markup (which is inherently a string) and generates an object for a run-time representation. Custom type authors that intend to support a type conversion behavior for XAML typically implement a TypeConverter class that supports their own unique ConvertFrom behavior from a string. This behavior enables type conversion from the string provided as a XAML attribute value and provides a XAML processor with the support needed to create an object from the string, so that the object can be produced in a parsed object graph. Custom types or members of custom types are indicated by applying TypeConverterAttribute to the definitions, with the attribute referencing the custom TypeConverter implementation. For more information, see Type Converters for XAML Overview.

Notes to Inheritors:

Inherit from TypeConverter to implement your own conversion requirements. When you inherit from this class, you can override the following methods:

System_CAPS_noteNote

Your derived type might be marked as internal or private, but an instance of your type can be created with the TypeDescriptor class. Do not write insecure code by assuming the caller is trusted. Assume instead that callers might create instances of your type in partial trust.

For more information about type converters for general (non-XAML) purposes, see How to: Implement a Type Converter or Generalized Type Conversion.

The following code example shows how to create an instance of a type converter and bind it to a class. The class implementing the converter, MyClassConverter, must inherit from the TypeConverter class.

[TypeConverter(typeof(MyClassConverter))]
 public class MyClass {
    // Insert code here.
}

When you have a property that has an enumeration, check to see whether an enumeration value is valid before setting the property. The next code example requires that an enumeration called MyPropertyEnum has been declared.

public MyPropertyEnum MyProperty {
    set {
       // Checks to see if the value passed is valid.
       if (!TypeDescriptor.GetConverter(typeof(MyPropertyEnum)).IsValid(value)) {
          throw new ArgumentException();
       }
       // The value is valid. Insert code to set the property.
    }
 }

Another common type converter usage is to convert an object to a string. The following code example prints out the name of the Color stored in the variable c.

Color c = Color.Red;
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));

You can also use a type converter to convert a value from its name, as shown in the next code example.

Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");

In the following code example, you can use a type converter to print out the set of standard values that the object supports.

foreach(Color c in TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues()) {
    Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
 }

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft