コード例 #1
0
        /// <summary>
        /// ConvertFrom - attempt to convert to a FontWeight from the given object
        /// </summary>
        /// <exception cref="NotSupportedException">
        /// A NotSupportedException is thrown if the example object is null or is not a valid type
        /// which can be converted to a FontWeight.
        /// </exception>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string s)
            {
                if (Enum.TryParse(s, true, out FontweightsEnum fontCode))
                {
                    return(FontWeight.INTERNAL_ConvertFromUshort((ushort)fontCode));
                }
                else if (ushort.TryParse(s, out ushort code))
                {
                    return(FontWeight.INTERNAL_ConvertFromUshort(code));
                }
                else
                {
                    throw new FormatException("Token is not valid.");
                }
            }

            throw GetConvertFromException(value);
        }