解决方案 »

  1.   

    之前回答你问题被你忽略了。你根本不懂原理。光重写tostring,那个看着差不多,其实没用。你还是需要实现typeconverter。
      

  2.   


    你好,我已经实现了typeconverter
    如下是代码:public class EdgesTypeConverter : TypeConverter
        {
            public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
            {
                return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
            }        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            {
                return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
            }        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                string text = value as string;
                if (text == null)
                {
                    return base.ConvertFrom(context, culture, value);
                }
                string text2 = text.Trim();
                if (text2.Length == 0)
                {
                    return null;
                }
                if (culture == null)
                {
                    culture = CultureInfo.CurrentCulture;
                }
                char c = culture.TextInfo.ListSeparator[0];
                string[] array = text2.Split(new char[] { c });
                int[] array2 = new int[array.Length];
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
                for (int i = 0; i < array2.Length; i++)
                {
                    array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
                }
                if (array2.Length == 4)
                {
                    return new Edges(array2[0], array2[1], array2[2], array2[3]);
                }
                throw new ArgumentException();
            }        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (destinationType == null)
                {
                    throw new ArgumentNullException("destinationType");
                }
                if (value is Edges)
                {
                    if (destinationType == typeof(string))
                    {
                        Edges edg = (Edges)value;
                        if (culture == null)
                        {
                            culture = CultureInfo.CurrentCulture;
                        }
                        string separator = culture.TextInfo.ListSeparator + " ";
                        TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
                        string[] array = new string[4];
                        int num = 0;
                        array[num++] = converter.ConvertToString(context, culture, edg.Left);
                        array[num++] = converter.ConvertToString(context, culture, edg.Top);
                        array[num++] = converter.ConvertToString(context, culture, edg.Right);
                        array[num++] = converter.ConvertToString(context, culture, edg.Bottom);
                        return string.Join(separator, array);
                    }
                    if (destinationType == typeof(InstanceDescriptor))
                    {
                        Edges edg2 = (Edges)value;
                        ConstructorInfo constructor = typeof(Edges).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) });
                        if (constructor != null)
                        {
                            return new InstanceDescriptor(constructor, new object[] { edg2.Left, edg2.Top, edg2.Right, edg2.Bottom });
                        }
                    }
                }
                return base.ConvertTo(context, culture, value, destinationType);
            }        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
            {
                if (propertyValues == null)
                {
                    throw new ArgumentNullException("propertyValues");
                }
                object obj = propertyValues["Left"];
                object obj2 = propertyValues["Top"];
                object obj3 = propertyValues["Right"];
                object obj4 = propertyValues["Bottom"];
                if (obj == null || obj2 == null || obj3 == null || obj4 == null || !(obj is int) || !(obj2 is int) || !(obj3 is int) || !(obj4 is int))
                {
                    throw new ArgumentException("PropertyValueInvalidEntry");
                }
                return new Edges((int)obj, (int)obj2, (int)obj3, (int)obj4);
            }        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
            {
                return true;
            }        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Edges), attributes);
                return properties.Sort(new string[] { "Left", "Top", "Right", "Bottom" });
            }        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return true;
            }
        }