DekGenius.com
[ Team LiB ] Previous Section Next Section

TypeConverter

System.ComponentModel (system.dll)class

This class provides a means to translate between one type and other representations of that type—typically a string representation. The designer environment uses type converters to translate between types it does not understand (for example, a System.Drawing.Size) and one it can represent in a System.Windows.Forms.PropertyGrid (for example, a string).

One of the quickest and simplest ways to make your custom type available in a designer is to implement a TypeConverter for it and adorn it with a TypeConverterAttribute to bind the appropriate converter. For finer control, you can add the attribute to a particular property to change the type converter for that particular instance of the type.

To implement a TypeConverter you should override the CanConvertFrom(), CanConvertTo( ), ConvertFrom() and ConvertTo() methods. At a minimum, you should implement conversion to and from a string, and you may also want to support InstanceDescriptor to support more complex initialization scenarios in design-time serialization.

If your object is immutable and requires recreation to modify it, you need to override CreateInstance() and GetCreateInstanceSupported(). This will be passed a System.Collections.IDictionary of property name/value pairs and optionally an ITypeDescriptorContext, which you may need to use in the conversion process.

If the type contains properties or you wish to extend it to appear to support properties of its own, you can override the GetProperties() and GetPropertiesSupported() methods. If you want add your own properties, derive them from SimplePropertyDescriptor (overriding the GetValue() and SetValue() methods.

A type can also support standard values (well-known values that can be assigned to the type). Implement GetStandardValues( ) and GetStandardValuesSupported() to provide standard values. If you also override GetStandardValuesExclusive(), you can indicate that the type will only accept one of the standard values.

public class TypeConverter {
// Public Constructors
   public TypeConverter();
// Public Instance Methods
   public virtual bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType);
   public bool CanConvertFrom(Type sourceType);
   public virtual bool CanConvertTo(ITypeDescriptorContext context, Type destinationType);
   public bool CanConvertTo(Type destinationType);
   public virtual object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value);
   public object ConvertFrom(object value);
   public object ConvertFromInvariantString(ITypeDescriptorContext context, string text);
   public object ConvertFromInvariantString(string text);
   public object ConvertFromString(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, string text);
   public object ConvertFromString(ITypeDescriptorContext context, string text);
   public object ConvertFromString(string text);
   public virtual object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, 
        object value, Type destinationType);
   public object ConvertTo(object value, Type destinationType);
   public string ConvertToInvariantString(ITypeDescriptorContext context, object value);
   public string ConvertToInvariantString(object value);
   public string ConvertToString(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value);
   public string ConvertToString(ITypeDescriptorContext context, object value);
   public string ConvertToString(object value);
   public object CreateInstance(System.Collections.IDictionary propertyValues);
   public virtual object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues);
   public bool GetCreateInstanceSupported();
   public virtual bool GetCreateInstanceSupported(ITypeDescriptorContext context);
   public PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value);
   public virtual PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, 
        Attribute[] attributes);
   public PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, object value);
   public bool GetPropertiesSupported();
   public virtual bool GetPropertiesSupported(ITypeDescriptorContext context);
   public ICollection GetStandardValues();
   public virtual StandardValuesCollection GetStandardValues(ITypeDescriptorContext context);
   public bool GetStandardValuesExclusive();
   public virtual bool GetStandardValuesExclusive(ITypeDescriptorContext context);
   public bool GetStandardValuesSupported();
   public virtual bool GetStandardValuesSupported(ITypeDescriptorContext context);
   public virtual bool IsValid(ITypeDescriptorContext context, object value);
   public bool IsValid(object value);
// Protected Instance Methods
   protected Exception GetConvertFromException(object value);
   protected Exception GetConvertToException(object value, Type destinationType);
   protected PropertyDescriptorCollection SortProperties(PropertyDescriptorCollection props, string[] names);
}

Subclasses

Multiple types

Returned By

IComNativeDescriptorHandler.GetConverter(), ICustomTypeDescriptor.GetConverter(), PropertyDescriptor.Converter, TypeDescriptor.GetConverter()

    [ Team LiB ] Previous Section Next Section