/// <summary> /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be /// used as generic method and casting is not required. /// <example> /// typeconverter.ConvertTo<int>(value); /// </example> /// </summary> public static T ConvertTo <T>(this NullableConverter typeconverter, Object value) { if (typeconverter == null) { throw new ArgumentNullException("typeconverter"); } return((T)typeconverter.ConvertTo(value, typeof(T))); }
/// <summary> /// Extends ConvertTo so that methods that return a specific type object given a Type parameter can be /// used as generic method and casting is not required. /// <example> /// nullableconverter.ConvertTo<int>(context, culture, value); /// </example> /// </summary> public static T ConvertTo <T>(this NullableConverter nullableconverter, ITypeDescriptorContext context, System.Globalization.CultureInfo culture, Object value) { if (nullableconverter == null) { throw new ArgumentNullException("nullableconverter"); } return((T)nullableconverter.ConvertTo(context, culture, value, typeof(T))); }
public static object ChangeType(object value, Type type, IFormatProvider provider) { if (type.IsOfType(typeof(Nullable <>))) { var nullableConverter = new NullableConverter(type); return(nullableConverter.ConvertTo(value, nullableConverter.UnderlyingType)); } return(Convert.ChangeType(value, type, provider)); }
private static bool TryConvert(Type destinationType, object value, out object result) { if (destinationType.IsGenericNullable()) { try { var converter = new NullableConverter(destinationType); result = converter.ConvertTo(value, converter.UnderlyingType); return(true); } }
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))); }
protected int Compare(T left, T right) { if (left is IComparable <T> comparable) { return(comparable.CompareTo(right)); } if (Equals(left, right)) { return(0); } if (ReferenceEquals(left, null)) { return(-1); } if (ReferenceEquals(right, null)) { return(1); } return((int)Comparer.Invoke(NullableConverter.ConvertTo(left, RawType), new[] { NullableConverter.ConvertTo(right, RawType) })); }