In the previous article, I added a complex property to the control and wrote a type converter for the type of the property, now let's look at the code for the type converter and explain the meaning of the code.
To implement a type converter, we have to rewrite (override) Four methods:
CanConvertFrom ()--tests based on the type parameters to determine whether it can be converted from this type to the current type, in this case we only provide the ability to convert string and InstanceDescriptor types.
CanConvertTo ()-tests based on the type parameters to determine whether it can be converted from the current type to the specified type.
ConvertTo ()-Converts the value of the parameter value to the specified type.
ConvertFrom ()-Swop parameter value and returns an object of the book type.
public override Object ConvertTo (ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, Object value, Type destinationtype)
{
String result = ' ";
if (destinationtype = = typeof (String))
{
Scope scope = (scope) value;
result = scope. Min.tostring () + "," + scope. Max.tostring ();
return result;
}
if (destinationtype = = typeof (InstanceDescriptor))
{
Constructori Nfo ci = typeof (Scope). GetConstructor (new type[]
{typeof (Int32), typeof (Int32)});
Scope scope = (scope) value;
return new InstanceDescriptor (CI, new object[] {
scope. Min,scope. Max});
Return to
base. ConvertTo (context, culture, value, destinationtype);
}