System.Drawing.PointConverter.ConvertTo C# (CSharp) Method

ConvertTo() public method

public ConvertTo ( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType ) : object
context ITypeDescriptorContext
culture System.Globalization.CultureInfo
value object
destinationType Type
return object
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            if (value is Point)
            {
                if (destinationType == typeof(string))
                {
                    Point pt = (Point)value;

                    if (culture == null)
                    {
                        culture = CultureInfo.CurrentCulture;
                    }
                    string sep = culture.TextInfo.ListSeparator + " ";
                    TypeConverter intConverter = TypeDescriptor.GetConverter(typeof(int));
                    string[] args = new string[2];
                    int nArg = 0;

                    // Note: ConvertToString will raise exception if value cannot be converted.
                    args[nArg++] = intConverter.ConvertToString(context, culture, pt.X);
                    args[nArg++] = intConverter.ConvertToString(context, culture, pt.Y);

                    return string.Join(sep, args);
                }
                
                if (destinationType == typeof(InstanceDescriptor))
                {
                    Point pt = (Point)value;

                    ConstructorInfo ctor = typeof(Point).GetConstructor(new Type[] { typeof(int), typeof(int) });
                    if (ctor != null)
                    {
                        return new InstanceDescriptor(ctor, new object[] { pt.X, pt.Y });
                    }
                }
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }

Usage Example

示例#1
0
文件: GLine.cs 项目: prosardar/BAD
 public GLine(Point aPoint1, Point aPoint2)
 {
     PointConverter aPointConverter = new PointConverter();
     FPoint1 = (PointF)aPointConverter.ConvertTo(aPoint1, typeof(PointF));
     FPoint2 = (PointF)aPointConverter.ConvertTo(aPoint2, typeof(PointF));
 }