using System; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Drawing; using System.Globalization; using System.Reflection; namespace CoreUnit.Common { public class RectangleFConverter : 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 str = value as string; if (value == null) return base.ConvertFrom(context, culture, value); str = str.Trim(); if (str.Length == 0) return null; if (culture == null) culture = CultureInfo.CurrentCulture; char ch = culture.TextInfo.ListSeparator[0]; string[] strArray = str.Split(new char[] { ch }); float[] numArray = new float[strArray.Length]; TypeConverter converter = TypeDescriptor.GetConverter(typeof(float)); for (int i = 0; i < numArray.Length; i++) { numArray[i] = (float)converter.ConvertFromString(context, culture, strArray[i]); } if (numArray.Length != 4) throw new ArgumentException("Invalid format"); return RectangleF.FromLTRB(numArray[0], numArray[1], numArray[2], numArray[3]); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) throw new ArgumentNullException("destinationType"); if (value is RectangleF) { if (destinationType == typeof(string)) { RectangleF rect = (RectangleF)value; if (culture == null) culture = CultureInfo.CurrentCulture; string separator = culture.TextInfo.ListSeparator + " "; TypeConverter converter = TypeDescriptor.GetConverter(typeof(float)); string[] strArray = new string[4]; int num = 0; strArray[num++] = converter.ConvertToString(context, culture, rect.Left); strArray[num++] = converter.ConvertToString(context, culture, rect.Top); strArray[num++] = converter.ConvertToString(context, culture, rect.Right); strArray[num++] = converter.ConvertToString(context, culture, rect.Bottom); return string.Join(separator, strArray); } if (destinationType == typeof(InstanceDescriptor)) { RectangleF rect2 = (RectangleF)value; ConstructorInfo constructor = typeof(RectangleF).GetConstructor(new Type[] { typeof(float), typeof(float), typeof(float), typeof(float) }); if (constructor != null) return new InstanceDescriptor(constructor, new object[] { rect2.Left, rect2.Top, rect2.Right, rect2.Bottom }); } } return base.ConvertTo(context, culture, value, destinationType); } public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) { if (propertyValues == null) throw new ArgumentNullException("propertyValues"); object leftvalue = propertyValues["Left"]; object topvalue = propertyValues["Top"]; object rightvalue = propertyValues["Right"]; object bottomvalue = propertyValues["Bottom"]; if (((leftvalue == null) || (topvalue == null) || (rightvalue == null) || (bottomvalue == null)) || (!(leftvalue is float) || !(topvalue is float) || !(rightvalue is float) || !(bottomvalue is float))) { throw new ArgumentException("Invalid property value entry"); } return RectangleF.FromLTRB((float)leftvalue, (float)topvalue, (float)rightvalue, (float)bottomvalue); } public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) { return true; } public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { return TypeDescriptor.GetProperties(typeof(RectangleF), attributes).Sort(new string[] { "Left", "Top", "Right", "Bottom" }); } public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return true; } } }