Provides a unified way of converting types of values to other types, as well as for accessing standard values and subproperties.
<ComVisibleAttribute(True)> _ <HostProtectionAttribute(SecurityAction.LinkDemand, SharedState := True)> _ Public Class TypeConverter
Dim instance As TypeConverter
[ComVisibleAttribute(true)] [HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)] public class TypeConverter
[ComVisibleAttribute(true)] [HostProtectionAttribute(SecurityAction::LinkDemand, SharedState = true)] public ref class TypeConverter
public class TypeConverter
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.
Never access a type converter directly. Instead, access the appropriate converter by using TypeDescriptor. For more information, see the code examples provided.
In Windows Presentation Foundation (WPF), the XAML loader searches for the TypeConverterAttribute directly, instead of going through TypeDescriptor.
Inherit from TypeConverter to implement your own conversion requirements. When you inherit from this class, you can override the following methods:
To support custom type conversion, override the CanConvertFrom, CanConvertTo, ConvertFrom, and ConvertTo methods.
To convert types that must re-create the object to change its value, override the CreateInstance and GetCreateInstanceSupported methods.
To convert types that support properties, override the GetProperties and GetPropertiesSupported methods. If the class you are converting does not have properties, and you need to implement properties, you can use the TypeConverter..::.SimplePropertyDescriptor class as a base for implementing the property descriptors. When you inherit from TypeConverter..::.SimplePropertyDescriptor, you must override the GetValue and SetValue methods.
To convert types that support standard values, override the GetStandardValues, GetStandardValuesExclusive, GetStandardValuesSupported and IsValid methods.
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.
<TypeConverter(GetType(MyClassConverter))> _ Public Class Class1 ' Insert code here. End Class 'MyClass
[TypeConverter(typeof(MyClassConverter))] public class MyClass { // Insert code here. }
public: [TypeConverter(Sample::MyClassConverter::typeid)] ref 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 WriteOnly Property MyProperty() As MyPropertyEnum Set ' Checks to see if the value passed is valid. If Not TypeDescriptor.GetConverter(GetType(MyPropertyEnum)).IsValid(value) Then Throw New ArgumentException() End If ' The value is valid. Insert code to set the property. End Set End Property
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. } }
public: property MyPropertyEnum MyProperty { void set( MyPropertyEnum value ) { // Checks to see if the value passed is valid. if ( !TypeDescriptor::GetConverter( MyPropertyEnum::typeid )->IsValid( value ) ) { throw gcnew 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.
Dim c As Color = Color.Red Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c))
Color c = Color.Red; Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(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.
Dim c As Color = CType(TypeDescriptor.GetConverter(GetType(Color)).ConvertFromString("Red"), Color)
Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
Color c = (Color)(TypeDescriptor::GetConverter( Color::typeid )->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.
Dim c As Color For Each c In TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues() Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c)) Next c
foreach(Color c in TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues()) { Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c)); }
for each ( Color c in TypeDescriptor::GetConverter( Color::typeid )->GetStandardValues() ) { Console::WriteLine( TypeDescriptor::GetConverter( c )->ConvertToString( c ) ); }
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