| 246 | } |
| 247 | |
| 248 | public class RectangleFConverter : TypeConverter |
| 249 | { |
| 250 | // Methods |
| 251 | public RectangleFConverter() |
| 252 | { |
| 253 | } |
| 254 | public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) |
| 255 | { |
| 256 | return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType)); |
| 257 | } |
| 258 | public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) |
| 259 | { |
| 260 | return ((destinationType == typeof(RectangleF)) || base.CanConvertTo(context, destinationType)); |
| 261 | } |
| 262 | public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) |
| 263 | { |
| 264 | if (culture == null) |
| 265 | culture = CultureInfo.CurrentCulture; |
| 266 | |
| 267 | if (!(value is string)) |
| 268 | return base.ConvertFrom(context, culture, value); |
| 269 | |
| 270 | string text = ((string)value).Trim(); |
| 271 | if (text.Length == 0) |
| 272 | return null; |
| 273 | |
| 274 | char ch = culture.TextInfo.ListSeparator[0]; |
| 275 | string[] textArray = text.Split(new char[] { ch }); |
| 276 | float[] numArray = new float[textArray.Length]; |
| 277 | if (numArray.Length != 4) |
| 278 | throw new ArgumentException("格式不正确"); |
| 279 | |
| 280 | TypeConverter converter = TypeDescriptor.GetConverter(typeof(float)); |
| 281 | for (int i = 0; i < numArray.Length; i++) |
| 282 | numArray[i] = (float)converter.ConvertFromString(context, culture, textArray[i]); |
| 283 | |
| 284 | return new RectangleF(numArray[0], numArray[1], numArray[2], numArray[3]); |
| 285 | } |
| 286 | public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) |
| 287 | { |
| 288 | if (culture == null) |
| 289 | culture = CultureInfo.CurrentCulture; |
| 290 | if (destinationType == null) |
| 291 | throw new ArgumentNullException("destinationType"); |
| 292 | |
| 293 | if ((destinationType == typeof(string)) && (value is RectangleF)) |
| 294 | { |
| 295 | RectangleF pointf = (RectangleF)value; |
| 296 | |
| 297 | string separator = culture.TextInfo.ListSeparator + " "; |
| 298 | TypeConverter converter = TypeDescriptor.GetConverter(typeof(float)); |
| 299 | string[] textArray = new string[4]; |
| 300 | int num = 0; |
| 301 | textArray[num++] = converter.ConvertToString(context, culture, pointf.X); |
| 302 | textArray[num++] = converter.ConvertToString(context, culture, pointf.Y); |
| 303 | textArray[num++] = converter.ConvertToString(context, culture, pointf.Width); |
| 304 | textArray[num++] = converter.ConvertToString(context, culture, pointf.Height); |
| 305 | return string.Join(separator, textArray); |
nothing calls this directly
no outgoing calls
no test coverage detected