Custom Type Converter for Adapter Configuration

Like the custom editor, the custom type converter overrides the System.ComponentModel.TypeConverter class of one of its children. Here, the converter adds formatting to the value to be persisted but does not appear on the property page. The ConvertFrom method adds square brackets around the string value and the ConvertTo method removes them.

The following code is the class definition for the custom type converter:

using System;  
using System.ComponentModel;  
  
namespace AdapterManagement.ComponentModel {  
  
   public class DesignerTypeConverter : StringConverter {  
  
      public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {  
         return (typeof(String) == destinationType) || base.CanConvertTo (context, destinationType);  
      }  
  
      public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {  
         if (typeof(String) == destinationType && value is String) {  
            return ((String)value).TrimStart('[').TrimEnd(']');  
         }  
         return base.ConvertTo (context, culture, value, destinationType);  
      }  
  
      public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {  
         return (typeof(String) == sourceType) || base.CanConvertFrom (context, sourceType);  
      }  
  
      public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {  
         if (value is String) {  
            return "["+(String)value+"]";  
         }  
         return base.ConvertFrom (context, culture, value);  
      }  
   }  
}  

See Also

Custom Adapter Configuration Designer
Custom Drop-Down Editor for Adapter Configuration
Custom Modal Dialog Editor for Adapter Configuration
Advanced Configuration Components for Adapters