Ejemplo n.º 1
0
        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 2 integer values.
                if (culture == null)
                {
                    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 == 2)
                {
                    return(new Point(values[0], values[1]));
                }
                else
                {
                    throw new ArgumentException(SR.Format(SR.TextParseFailedFormat, text, "x, y"));
                }
            }

            return(base.ConvertFrom(context, culture, value));
        }
Ejemplo n.º 2
0
        public override object?ConvertTo(ITypeDescriptorContext?context, CultureInfo?culture, object?value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            if (value is SizeF size)
            {
                if (destinationType == typeof(string))
                {
                    if (culture == null)
                    {
                        culture = CultureInfo.CurrentCulture;
                    }

                    string        sep            = culture.TextInfo.ListSeparator + " ";
                    TypeConverter floatConverter = TypeDescriptor.GetConverterTrimUnsafe(typeof(float));
                    var           args           = new string?[]
                    {
                        floatConverter.ConvertToString(context, culture, size.Width),
                        floatConverter.ConvertToString(context, culture, size.Height)
                    };
                    return(string.Join(sep, args));
                }
                else if (destinationType == typeof(InstanceDescriptor))
                {
                    ConstructorInfo?ctor = typeof(SizeF).GetConstructor(new Type[] { typeof(float), typeof(float) });
                    if (ctor != null)
                    {
                        return(new InstanceDescriptor(ctor, new object[] { size.Width, size.Height }));
                    }
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
Ejemplo n.º 3
0
        public override object?ConvertTo(ITypeDescriptorContext?context, CultureInfo?culture, object?value, Type destinationType)
        {
            ArgumentNullException.ThrowIfNull(destinationType);

            if (value is Size size)
            {
                if (destinationType == typeof(string))
                {
                    if (culture == null)
                    {
                        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, size.Width),
                        intConverter.ConvertToString(context, culture, size.Height)
                    };
                    return(string.Join(sep, args));
                }
                else if (destinationType == typeof(InstanceDescriptor))
                {
                    ConstructorInfo?ctor = typeof(Size).GetConstructor(new Type[] { typeof(int), typeof(int) });
                    if (ctor != null)
                    {
                        return(new InstanceDescriptor(ctor, new object[] { size.Width, size.Height }));
                    }
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
Ejemplo n.º 4
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(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 = null;
                    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));
        }