System.ComponentModel.NullableConverter.ConvertTo C# (CSharp) Метод

ConvertTo() публичный Метод

public ConvertTo ( ITypeDescriptorContext context, System culture, object value, Type destinationType ) : object
context ITypeDescriptorContext
culture System
value object
destinationType System.Type
Результат object
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
            if (destinationType == null) {
                throw new ArgumentNullException("destinationType");
            }

            if (destinationType == this.simpleType && this.nullableType.IsInstanceOfType(value)) {
                return value;
            }
            else if (destinationType == typeof(InstanceDescriptor)) {
                ConstructorInfo ci = nullableType.GetConstructor(new Type[] {simpleType});
                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 (this.simpleTypeConverter != null) {
                return this.simpleTypeConverter.ConvertTo(context, culture, value, destinationType);
            }
            
            return base.ConvertTo(context, culture, value, destinationType);
        }

Usage Example

        public ConversionResult Convert(ConversionContext conversion)
        {
            if (conversion.HasValue==false)
                return conversion.Unconverted();

            var converter=new NullableConverter(typeof(Nullable<>).MakeGenericType(conversion.GetType()));
            return conversion.Result(converter.ConvertTo(conversion, converter.UnderlyingType));
        }