Example #1
0
        /// <summary>
        /// Converts the given value object to the destination type.
        /// </summary>
        public override object?ConvertTo(ITypeDescriptorContext?context, CultureInfo?culture, object?value, Type destinationType)
        {
            ArgumentNullException.ThrowIfNull(destinationType);

            if (destinationType == UnderlyingType && value != null && NullableType.IsInstanceOfType(value))
            {
                return(value);
            }
            else if (destinationType == typeof(InstanceDescriptor))
            {
                ConstructorInfo ci = (ConstructorInfo)NullableType.GetMemberWithSameMetadataDefinitionAs(s_nullableConstructor);
                Debug.Assert(ci != null, "Couldn't find constructor");
                return(new InstanceDescriptor(ci, new object?[] { value }, true));
            }
            else if (value == null)
            {
                // Handle our own nulls here
                if (destinationType == typeof(string))
                {
                    return(string.Empty);
                }
            }
            else if (UnderlyingTypeConverter != null)
            {
                return(UnderlyingTypeConverter.ConvertTo(context, culture, value, destinationType));
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
Example #2
0
        /// <summary>
        /// Converts the given value object to the destination type.
        /// </summary>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException(nameof(destinationType));
            }

            if (destinationType == UnderlyingType && value != null && NullableType.IsInstanceOfType(value))
            {
                return(value);
            }
            else if (destinationType == typeof(InstanceDescriptor))
            {
                ConstructorInfo ci = NullableType.GetConstructor(new Type[] { UnderlyingType });
                Debug.Assert(ci != null, "Couldn't find constructor");
                return(new InstanceDescriptor(ci, new object[] { value }, true));
            }
            else if (value == null)
            {
                // Handle our own nulls here
                if (destinationType == typeof(string))
                {
                    return(string.Empty);
                }
            }
            else if (UnderlyingTypeConverter != null)
            {
                return(UnderlyingTypeConverter.ConvertTo(context, culture, value, destinationType));
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
            /// <summary>
            /// Converts the given value object to the destination type.
            /// </summary>
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (destinationType == null)
                {
                    throw new ArgumentNullException(nameof(destinationType));
                }

                if (destinationType == UnderlyingType && value != null && NullableType.IsInstanceOfType(value))
                {
                    return(value);
                }
                else if (value == null)
                {
                    // Handle our own nulls here
                    if (destinationType == typeof(string))
                    {
                        return(string.Empty);
                    }
                }
                else if (UnderlyingTypeConverter != null)
                {
                    return(UnderlyingTypeConverter.ConvertTo(context, culture, value, destinationType));
                }

                return(base.ConvertTo(context, culture, value, destinationType));
            }
Example #4
0
        public override object ConvertTo(CultureInfo culture, string format, object value, Type to)
        {
            if (to == UnderlyingType && NullableType.IsInstanceOfType(value))
            {
                return(value);
            }

            if (value == null)
            {
                if (to == typeof(string))
                {
                    return(string.Empty);
                }
            }

            return(UnderlyingTypeConverter.ConvertTo(culture, format, value, to));
        }