Exemplo n.º 1
0
        /// <summary>
        /// Converts the given object to the type of this converter, using the specified context and culture information.
        /// </summary>
        /// <param name="context">An <see cref="ITypeDescriptorContext"/> that provides a format context.</param>
        /// <param name="culture">The <see cref="CultureInfo"/> to use as the current culture.</param>
        /// <param name="value">The <see cref="Object"/> to convert.</param>
        /// <returns>An <see cref="Object"/> that represents the converted value.</returns>
        /// <exception cref="NotSupportedException">The conversion cannot be performed.</exception>
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var asByteArray = value as byte[];
            var asString    = value as string;

            if (asByteArray != null)
            {
                return(new Ulid(asByteArray));
            }

            if (asString != null)
            {
                if (Ulid.TryParse(asString, out Ulid ulid))
                {
                    return(ulid);
                }
                throw new NotSupportedException($"Invalid Ulid representation: \"{asString}\"");
            }

            return(base.ConvertFrom(context, culture, value));
        }