/// <include file='doc\ColorConverter.uex' path='docs/doc[@for="ColorConverter.ConvertTo"]/*' /> /// <devdoc> /// Converts the given object to another type. The most common types to convert /// are to and from a string object. The default implementation will make a call /// to ToString on the object if the object is valid and if the destination /// type is string. If this cannot convert to the desitnation type, this will /// throw a NotSupportedException. /// </devdoc> public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (value is Color) { if (destinationType == typeof(string)) { Color c = (Color)value; if (c == Color.Empty) { return(string.Empty); } else { // If this is a known color, then Color can provide its own // name. Otherwise, we fabricate an ARGB value for it. // if (ColorExt.IsKnownColor(c)) { return(c.Name); } else if (c.IsNamedColor) { return("'" + c.Name + "'"); } else { if (culture == null) { culture = CultureInfo.CurrentCulture; } string sep = culture.TextInfo.ListSeparator + " "; TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int)); string[] args; int nArg = 0; if (c.A < 255) { args = new string[4]; args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.A); } else { args = new string[3]; } // Note: ConvertToString will raise exception if value cannot be converted. args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.R); args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.G); args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.B); // Now slam all of these together with the fantastic Join // method. // return(string.Join(sep, args)); } } } if (destinationType == typeof(InstanceDescriptor)) { MemberInfo member = null; object[] args = null; Color c = (Color)value; if (c.IsEmpty) { member = typeof(Color).GetField("Empty"); } else if (ColorExt.IsSystemColor(c)) { member = typeof(SystemColors).GetProperty(c.Name); } else if (ColorExt.IsKnownColor(c)) { member = typeof(Color).GetProperty(c.Name); } else if (c.A != 255) { member = typeof(Color).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }); args = new object[] { c.A, c.R, c.G, c.B }; } else if (c.IsNamedColor) { member = typeof(Color).GetMethod("FromName", new Type[] { typeof(string) }); args = new object[] { c.Name }; } else { member = typeof(Color).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int) }); args = new object[] { c.R, c.G, c.B }; } //Debug.Assert(member != null, "Could not convert color to member. Did someone change method name / signature and not update Colorconverter?"); if (member != null) { return(new InstanceDescriptor(member, args)); } else { return(null); } } } return(base.ConvertTo(context, culture, value, destinationType)); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (value is Color) { if (destinationType == typeof(string)) { string[] strArray; Color color = (Color)value; if (color == Color.Empty) { return(string.Empty); } if (color.IsKnownColor) { return(color.Name); } if (color.IsNamedColor) { return("'" + color.Name + "'"); } if (culture == null) { culture = CultureInfo.CurrentCulture; } string separator = culture.TextInfo.ListSeparator + " "; TypeConverter converter = TypeDescriptor.GetConverter(typeof(int)); int num = 0; if (color.A < 0xff) { strArray = new string[4]; strArray[num++] = converter.ConvertToString(context, culture, color.A); } else { strArray = new string[3]; } strArray[num++] = converter.ConvertToString(context, culture, color.R); strArray[num++] = converter.ConvertToString(context, culture, color.G); strArray[num++] = converter.ConvertToString(context, culture, color.B); return(string.Join(separator, strArray)); } if (destinationType == typeof(InstanceDescriptor)) { MemberInfo member = null; object[] arguments = null; Color color2 = (Color)value; if (color2.IsEmpty) { member = typeof(Color).GetField("Empty"); } else if (color2.IsSystemColor) { member = typeof(System.Drawing.SystemColors).GetProperty(color2.Name); } else if (color2.IsKnownColor) { member = typeof(Color).GetProperty(color2.Name); } else if (color2.A != 0xff) { member = typeof(Color).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }); arguments = new object[] { color2.A, color2.R, color2.G, color2.B }; } else if (color2.IsNamedColor) { member = typeof(Color).GetMethod("FromName", new Type[] { typeof(string) }); arguments = new object[] { color2.Name }; } else { member = typeof(Color).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int) }); arguments = new object[] { color2.R, color2.G, color2.B }; } if (member != null) { return(new InstanceDescriptor(member, arguments)); } return(null); } } return(base.ConvertTo(context, culture, value, destinationType)); }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == (Type)null) { throw new ArgumentNullException("destinationType"); } if (value is Color) { if (destinationType == typeof(string)) { Color left = (Color)value; if (left == Color.Empty) { return(string.Empty); } /* if (left.IsKnownColor) * { * return left.Name; * } */ if (left.IsNamedColor) { return("'" + left.Name + "'"); } if (culture == null) { culture = CultureInfo.CurrentCulture; } string separator = culture.TextInfo.ListSeparator + " "; TypeConverter converter = TypeDescriptor.GetConverter(typeof(int)); int num = 0; string[] array; if (left.A < byte.MaxValue) { array = new string[4]; array[num++] = converter.ConvertToString(context, culture, left.A); } else { array = new string[3]; } array[num++] = converter.ConvertToString(context, culture, left.R); array[num++] = converter.ConvertToString(context, culture, left.G); array[num++] = converter.ConvertToString(context, culture, left.B); return(string.Join(separator, array)); } if (destinationType == typeof(InstanceDescriptor)) { MemberInfo memberInfo = null; object[] arguments = null; Color color = (Color)value; if (color.IsEmpty) { memberInfo = typeof(Color).GetField("Empty"); } /* else if (color.IsSystemColor) * { * memberInfo = typeof(SystemColors).GetProperty(color.Name); * } * else if (color.IsKnownColor) * { * memberInfo = typeof(Color).GetProperty(color.Name); * }*/ else if (color.A != byte.MaxValue) { memberInfo = typeof(Color).GetMethod("FromArgb", new Type[4] { typeof(int), typeof(int), typeof(int), typeof(int) }); arguments = new object[4] { color.A, color.R, color.G, color.B }; } else if (color.IsNamedColor) { memberInfo = typeof(Color).GetMethod("FromName", new Type[1] { typeof(string) }); arguments = new object[1] { color.Name }; } else { memberInfo = typeof(Color).GetMethod("FromArgb", new Type[3] { typeof(int), typeof(int), typeof(int) }); arguments = new object[3] { color.R, color.G, color.B }; } if (memberInfo != (MemberInfo)null) { return(new InstanceDescriptor(memberInfo, arguments)); } return(null); } } return(base.ConvertTo(context, culture, value, destinationType)); }
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) { ArgumentNullException.ThrowIfNull(destinationType); if (value is Color c) { if (destinationType == typeof(string)) { if (c == Color.Empty) { return string.Empty; } // If this is a known color, then Color can provide its own name. // Otherwise, we fabricate an ARGB value for it. if (ColorTable.IsKnownNamedColor(c.Name)) { return c.Name; } else if (c.IsNamedColor) { return "'" + c.Name + "'"; } if (culture == null) { culture = CultureInfo.CurrentCulture; } string sep = culture.TextInfo.ListSeparator + " "; TypeConverter intConverter = TypeDescriptor.GetConverterTrimUnsafe(typeof(int)); string?[] args; int nArg = 0; if (c.A < 255) { args = new string?[4]; args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.A); } else { args = new string[3]; } // Note: ConvertToString will raise exception if value cannot be converted. args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.R); args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.G); args[nArg++] = intConverter.ConvertToString(context, culture, (object)c.B); return string.Join(sep, args); } else if (destinationType == typeof(InstanceDescriptor)) { MemberInfo? member; object[]? args = null; if (c.IsEmpty) { member = typeof(Color).GetField("Empty"); } else if (ColorTable.IsKnownNamedColor(c.Name)) { member = typeof(Color).GetProperty(c.Name) ?? typeof(SystemColors).GetProperty(c.Name); } else if (c.A != 255) { member = typeof(Color).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }); args = new object[] { c.A, c.R, c.G, c.B }; } else if (c.IsNamedColor) { member = typeof(Color).GetMethod("FromName", new Type[] { typeof(string) }); args = new object[] { c.Name }; } else { member = typeof(Color).GetMethod("FromArgb", new Type[] { typeof(int), typeof(int), typeof(int) }); args = new object[] { c.R, c.G, c.B }; } Debug.Assert(member != null, "Could not convert color to member. Did someone change method name / signature and not update ColorConverter?"); if (member != null) { return new InstanceDescriptor(member, args); } else { return null; } } } return base.ConvertTo(context, culture, value, destinationType); }