public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException(nameof(destinationType));
}
if (value is Rectangle)
{
if (destinationType == typeof(string))
{
Rectangle rect = (Rectangle)value;
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
string sep = culture.TextInfo.ListSeparator + " ";
TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
string[] args = new string[4];
int nArg = 0;
// Note: ConvertToString will raise exception if value cannot be converted.
args[nArg++] = intConverter.ConvertToString(context, culture, rect.X);
args[nArg++] = intConverter.ConvertToString(context, culture, rect.Y);
args[nArg++] = intConverter.ConvertToString(context, culture, rect.Width);
args[nArg++] = intConverter.ConvertToString(context, culture, rect.Height);
return string.Join(sep, args);
}
if (destinationType == typeof(InstanceDescriptor))
{
Rectangle rect = (Rectangle)value;
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);
}