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

            if (destinationType == typeof(string) && value != null)
            {
                PropertyInfo[] props     = GetProperties();
                int            bestMatch = -1;

                for (int i = 0; i < props.Length; i++)
                {
                    PropertyInfo prop      = props[i];
                    object[]     tempIndex = null;
                    Cursor       c         = (Cursor)prop.GetValue(null, tempIndex);
                    if (c == (Cursor)value)
                    {
                        if (Object.ReferenceEquals(c, value))
                        {
                            return(prop.Name);
                        }
                        else
                        {
                            bestMatch = i;
                        }
                    }
                }

                if (bestMatch != -1)
                {
                    return(props[bestMatch].Name);
                }

                // We throw here because we cannot meaningfully convert a custom
                // cursor into a string. In fact, the ResXResourceWriter will use
                // this exception to indicate to itself that this object should
                // be serialized through ISeriazable instead of a string.
                //
                throw new FormatException(SR.CursorCannotCovertToString);
            }

            if (destinationType == typeof(InstanceDescriptor) && value is Cursor)
            {
                PropertyInfo[] props = GetProperties();
                foreach (PropertyInfo prop in props)
                {
                    if (prop.GetValue(null, null) == value)
                    {
                        return(new InstanceDescriptor(prop, null));
                    }
                }
            }

            if (destinationType == typeof(byte[]))
            {
                if (value != null)
                {
                    MemoryStream ms     = new MemoryStream();
                    Cursor       cursor = (Cursor)value;
                    cursor.SavePicture(ms);
                    ms.Close();
                    return(ms.ToArray());
                }
                else
                {
                    return(Array.Empty <byte>());
                }
            }

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