Introduction to TypeConverters, Part I...

This article will explain the TypeConverter class and the theory behind it, but won't delve into the technical workings. Future article(s) will utilize the technology of the TypeConverter, its properties, and its methods.


By: Brian Mains Date: April 5, 2004

When I began using ASP.NET, I was amazed by the Visual Studio .NET designer and how easy it was to create custom styles and templates for controls. As I delved more into creating custom controls, I was amazed at how easy it was to create one and define your own properties. In addition, for properties that had a definable set of parameters, the field in the designer in Visual Studio .NET changed the field into a drop-down list or other suitable control. This functionality is defined in a TypeConverter, which is defined in the System.ComponentModel namespace. The TypeConverter is a class that defines the conversion between two differing types. The process is explained further below.

This article will explain the TypeConverter class and the theory behind it, but won't delve into the technical workings. Future article(s) will utilize the technology of the TypeConverter, its properties, and its methods.

The TypeConverter class, as stated before, creates a conversion of values between differing data types. Typically, this conversion is between a string value (this will be the assumption throughout this article) and a class object of some sort (defined in the .NET framework or a custom class in an application). For example, a Name class with First, Last, and Middle Initial properties would be converted to a string in the format of "<first name> <middle initial> <last name>." In this string, the space is used as a separator, which will be used to split the string apart and reconstruct the class. The string format can be any variety of formats, as long as it is predefined in the TypeConverter code that will handle the conversion. The primary methods used in the conversion are:

The CanConvertFrom() and CanConvertTo() methods check the values to make sure that they are appropriate for the conversion. Do any format validation here. Each of these methods takes two parameters, the second of which is the most important. This value is a System.Type object that is used to verify the type passed to/from the TypeConverter.

The ConvertFrom() and ConvertTo() methods handle the work in the conversion. The ConvertFrom() method receives an object, which is used to check that the its type meets the requirements. The object is then converted to a string, which is deconstructed and the data is passed to the class object. On the other hand, the ConvertTo() method receives an object that is the desired class object. A destination type parameter is used to verify that it is appropriate, followed by the conversion of a class to a string. In our Name class example, the parameters of this class would be converted to a string of "<First Name> <Middle Initial> <Last Name>", as shown earlier in the article. This back-and-forth conversion of data is what you will see in the designer when you see the custom property in the property window. Look real closely at the property (at the parent and child levels) when you look at the code in subsequent article(s).

There are many types of TypeConverters available, such as:

Any of these TypeConverters can be used or created through inheritance. The ConvertFrom() and ConvertTo() methods are then overridden to handle the custom conversion. To link the TypeConverter to the class, and also to the Visual Studio .NET designer, you must define attributes (also defined in the System.ComponentModel namespace) to handle the design-time functionality. These attributes must be declared in the control and in the class declaration. The typical attributes that you will declare are:

A specific example will be used, and all of the information will be explained in the next article, "Type Converters, Part II."