Design-Time Integration—Custom Type Converters
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| CSharp-Online.NET:Articles |
| C# Articles |
| © 2004 Chris Sells |
Custom Type Converters
Although the built-in type converters are useful, they aren't enough if your component or control exposes properties based on custom types, such as the clock control's HourHand, MinuteHand, and SecondHand properties, shown here:
public class Hand { public Hand(Color color, int width) { this.color = color; this.width = width; } public Color Color { get { return color; } set { color = value; } } public int Width { get { return width; } set { width = value; } } Color color = Color.Black; int width = 1; } public class ClockControl : Control { public Hand HourHand { ... } public Hand MinuteHand { ... } public Hand SecondHand { ... } }
The idea is to give developers the option to pretty up the clock's hands with color and width values. Without a custom type converter,5 the unfortunate result is shown in Figure 9.22.

Figure 9.22. Complex Properties in the Property Browser
Just as the Property Browser can't know which types it will be displaying, .NET can't know which custom types you'll be developing. Consequently, there aren't any type of converters capable of handling them. However, you can hook into the type converter infrastructure to provide your own. Building a custom type converter starts by deriving from the TypeConverter base class:
public class HandConverter : TypeConverter { ... }
To support conversion, HandConverter must override CanConvertFrom, ConvertTo, and ConvertFrom:
public class HandConverter : TypeConverter { public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType) {...} public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo info, object value) {...} public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {...} }
CanConvertFrom lets clients know what it can convert from. In this case, HandConverter reports that it can convert from a string type to a Hand type:
public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType) { // We can convert from a string to a Hand type if( sourceType == typeof(string) ) { return true; } return base.CanConvertFrom(context, sourceType); }
Whether the string type is in the correct format is left up to ConvertFrom, which actually performs the conversion. HandConverter expects a multivalued string. It splits this string into its atomic values and then uses it to instantiate a Hand object:
public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo info, object value) { // If converting from a string if( value is string ) { // Build a Hand type try { // Get Hand properties string propertyList = (string)value; string[] properties = propertyList.Split(';'); return new Hand(Color.FromName(properties[0].Trim()), int.Parse(properties[1])); } catch {} throw new ArgumentException("The arguments were not valid."); } return base.ConvertFrom(context, info, value); } ... }
ConvertTo converts from a Hand type back to a string:
public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { // If source value is a Hand type if( value is Hand ) { // Convert to string if( (destinationType == typeof(string)) ) { Hand hand = (Hand)value; string color = (hand.Color.IsNamedColor ? hand.Color.Name : hand.Color.R + ", " + hand.Color.G + ", " + hand.Color.B); return string.Format("{0}; {1}", color, hand.Width.ToString()); } } return base.ConvertTo(context, culture, value, destinationType); }
You may have noticed that HandConverter doesn't implement a CanConvertTo override. The base implementation of TypeConverter.CanConvertTo returns a Boolean value of true when queried for its ability to convert to a string type. Because this is the right behavior for HandConverter (and for most other custom type converters), there's no need to override it.
When the Property Browser looks for a custom type converter, it looks at each property for a TypeConverterAttribute:
public class ClockControl : Control { ... [ TypeConverterAttribute (typeof(HandConverter)) ] public Hand HourHand { ... } [TypeConverterAttribute (typeof(HandConverter)) ] public Hand MinuteHand { ... } [TypeConverterAttribute (typeof(HandConverter)) ] public Hand SecondHand { ... } ... }
However, this is somewhat cumbersome, so it's simpler to decorate the type itself with TypeConverterAttribute:
[ TypeConverterAttribute(typeof(HandConverter)) ] public class Hand { ... } public class ClockControl : Control { ... public Hand HourHand { ... } public Hand MinuteHand { ... } public Hand SecondHand { ... } ... }
Figure 9.23 shows the effect of the custom HandConverter type converter.

Figure 9.23. HandConverter in Action
|

