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)

public class TypeConverter

NoteNote:

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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.

NoteNote:

Never access a type converter directly. Instead, access the appropriate converter by using TypeDescriptor. For more information, see the code examples provided.

NoteNote:

In Windows Presentation Foundation (WPF), the XAML loader searches for the TypeConverterAttribute directly, instead of going through TypeDescriptor.

Notes to Inheritors:

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

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, 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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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

No code example is currently available or this language may not be supported.

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

No code example is currently available or this language may not be supported.

System.Object
  System.ComponentModel.TypeConverter
    Microsoft.VisualBasic.ApplicationServices.BuiltInRoleConverter
    System.ComponentModel.BaseNumberConverter
    System.ComponentModel.BooleanConverter
    System.ComponentModel.CharConverter
    System.ComponentModel.CollectionConverter
    System.ComponentModel.CultureInfoConverter
    System.ComponentModel.DateTimeConverter
    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.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.CornerRadiusConverter
    System.Windows.CultureInfoIetfLanguageTagConverter
    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.RoutedEventConverter
    System.Windows.Markup.TemplateKeyConverter
    System.Windows.Markup.XmlLanguageConverter
    System.Windows.Media.Animation.RepeatBehaviorConverter
    System.Windows.Media.BrushConverter
    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.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

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft