All pastes #967592 Raw Edit

Something

public text v1 · immutable
·published 2008-04-02 15:06 UTC
rendered paste body

    public class Converter : ExpandableObjectConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
                return true;

            return base.CanConvertFrom(context, sourceType);
        }
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType== typeof(string))
                return true;
            return base.CanConvertTo(context, destinationType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            string str = value as string;
            if(str == null)
                return base.ConvertFrom(context, culture, value);

            string[] items = str.Split(":".ToCharArray());
            if (items.Length != 2)
                return base.ConvertFrom(context, culture, value);

            MyObject p = context.PropertyDescriptor.GetValue(context.Instance) as MyObject;
            p.MyValue = int.Parse(items[0]);
        }
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            MyObject p = value as MyObject;
            if(p==null)
                return base.ConvertTo(context, culture, value, destinationType);

            return string.Format("{0}:{1}", p.MyValue, p.MyValue2);
        }
    }