/
RectangleConverter.cs
129 lines (106 loc) · 5.36 KB
/
RectangleConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace System.Drawing
{
public class RectangleConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen(true)] Type? destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
}
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value)
{
if (value is string strValue)
{
string text = strValue.Trim();
if (text.Length == 0)
{
return null;
}
// Parse 4 integer values.
culture ??= CultureInfo.CurrentCulture;
char sep = culture.TextInfo.ListSeparator[0];
string[] tokens = text.Split(sep);
int[] values = new int[tokens.Length];
TypeConverter intConverter = TypeDescriptor.GetConverterTrimUnsafe(typeof(int));
for (int i = 0; i < values.Length; i++)
{
// Note: ConvertFromString will raise exception if value cannot be converted.
values[i] = (int)intConverter.ConvertFromString(context, culture, tokens[i])!;
}
if (values.Length != 4)
{
throw new ArgumentException(SR.Format(SR.TextParseFailedFormat, text, "x, y, width, height"));
}
return new Rectangle(values[0], values[1], values[2], values[3]);
}
return base.ConvertFrom(context, culture, value);
}
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
ArgumentNullException.ThrowIfNull(destinationType);
if (value is Rectangle rect)
{
if (destinationType == typeof(string))
{
culture ??= CultureInfo.CurrentCulture;
string sep = culture.TextInfo.ListSeparator + " ";
TypeConverter intConverter = TypeDescriptor.GetConverterTrimUnsafe(typeof(int));
// Note: ConvertToString will raise exception if value cannot be converted.
var args = new string?[]
{
intConverter.ConvertToString(context, culture, rect.X),
intConverter.ConvertToString(context, culture, rect.Y),
intConverter.ConvertToString(context, culture, rect.Width),
intConverter.ConvertToString(context, culture, rect.Height)
};
return string.Join(sep, args);
}
else if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo? ctor = typeof(Rectangle).GetConstructor(new Type[] {
typeof(int), typeof(int), typeof(int), typeof(int)});
if (ctor != null)
{
return new InstanceDescriptor(ctor, new object[] {
rect.X, rect.Y, rect.Width, rect.Height});
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(ITypeDescriptorContext? context, IDictionary propertyValues)
{
ArgumentNullException.ThrowIfNull(propertyValues);
object? x = propertyValues["X"];
object? y = propertyValues["Y"];
object? width = propertyValues["Width"];
object? height = propertyValues["Height"];
if (x == null || y == null || width == null || height == null ||
!(x is int) || !(y is int) || !(width is int) || !(height is int))
{
throw new ArgumentException(SR.PropertyValueInvalidEntry);
}
return new Rectangle((int)x, (int)y, (int)width, (int)height);
}
public override bool GetCreateInstanceSupported(ITypeDescriptorContext? context) => true;
private static readonly string[] s_propertySort = { "X", "Y", "Width", "Height" };
[RequiresUnreferencedCode("The Type of value cannot be statically discovered. " + AttributeCollection.FilterRequiresUnreferencedCodeMessage)]
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext? context, object? value, Attribute[]? attributes)
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(Rectangle), attributes);
return props.Sort(s_propertySort);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext? context) => true;
}
}