예제 #1
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            ArgumentNullException.ThrowIfNull(destinationType);

            if (destinationType == typeof(string))
            {
                if (value is not null)
                {
                    string str = _com2Enum.ToString(value);
                    return(str ?? "");
                }
            }

            if (destinationType.IsEnum)
            {
                return(Enum.ToObject(destinationType, value));
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
예제 #2
0
        /// <include file='doc\COM2EnumConverter.uex' path='docs/doc[@for="Com2EnumConverter.ConvertTo"]/*' />
        /// <devdoc>
        ///      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.
        /// </devdoc>
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }

            if (destinationType == typeof(string))
            {
                if (value != null)
                {
                    string str = com2Enum.ToString(value);
                    return(str == null ? "" : str);
                }
            }

            if (destinationType.IsEnum)
            {
                return(Enum.ToObject(destinationType, value));
            }
            return(base.ConvertTo(context, culture, value, destinationType));
        }