public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { var targetType = convertTo; while (targetType != null) { var sourceType = value?.GetType() ?? typeof(void); while (sourceType != null) { var key = Tuple.Create(sourceType, targetType); ITypeConverter converter; if (typeConverters.TryGetValue(key, out converter)) { if (converter.TryConvertTo(root, context, convertTo, value, out result)) { return(true); } } sourceType = GetBaseType(sourceType); } targetType = GetBaseType(targetType); } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if ((Context & context) == context) { return Converter.TryConvertTo(root, context, convertTo, value, out result); } result = null; return false; }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if ((Context & context) == context) { return(Converter.TryConvertTo(root, context, convertTo, value, out result)); } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo == typeof(string)) { result = "bar"; return(true); } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo == typeof(string[]) && (value?.GetType().IsArray ?? false)) { result = new[] { string.Join(",", ((Array)value).Cast <object>().Select(x => root.ConvertTo <string>(context, x))) }; return(true); } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo.IsInstanceOfType(value)) { result = value; return true; } result = null; return false; }
/// <summary> /// Converts the specified value into the type indicated by <c>T</c>. The result of the conversion /// is stored in result and the value returned indicates whether conversion was successful.<br/> /// </summary> /// <param name="typeConverter">The type converter performing the conversion (i.e. "this").</param> /// <param name="context">The usage scenario of the type conversion (path, query string, etc.)</param> /// <param name="convertTo">The type to which the <c>value</c> should be converted.</param> /// <param name="value">The object that should be converted to <c>T</c>.</param> /// <returns>The <c>value</c> converted into type <c>T</c>.</returns> /// <exception cref="TypeConversionException">When the type converter could not convert the value to the specified /// type.</exception> public static object ConvertTo(this ITypeConverter typeConverter, TypeConversionContext context, Type convertTo, object value) { object result; if (!typeConverter.TryConvertTo(typeConverter, context, convertTo, value, out result)) { throw new TypeConversionException(value, convertTo); } return(result); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { foreach (var typeConverter in typeConverters) { if (typeConverter.TryConvertTo(root, context, convertTo, value, out result)) return true; } result = null; return false; }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo == typeof(string[]) && (value?.GetType().IsArray ?? false)) { result = new[] { string.Join(",", ((Array)value).Cast<object>().Select(x => root.ConvertTo<string>(context, x))) }; return true; } result = null; return false; }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo.IsInstanceOfType(value)) { result = value; return(true); } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { foreach (var typeConverter in typeConverters) { if (typeConverter.TryConvertTo(root, context, convertTo, value, out result)) { return(true); } } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object obj, out object result) { try { result = Convert.ChangeType(obj, convertTo); return(true); } catch (Exception) { result = null; return(false); } }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo.IsArray) { var array = Array.CreateInstance(convertTo.GetElementType(), 1); var typedValue = root.ConvertTo(context, convertTo.GetElementType(), value); array.SetValue(typedValue, 0); result = array; return(true); } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object obj, out object result) { try { result = Convert.ChangeType(obj, convertTo); return true; } catch (Exception) { result = null; return false; } }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo.IsArray) { var array = Array.CreateInstance(convertTo.GetElementType(), 1); var typedValue = root.ConvertTo(context, convertTo.GetElementType(), value); array.SetValue(typedValue, 0); result = array; return true; } result = null; return false; }
/// <summary> /// Converts the specified value into the type indicated by <c>T</c>. The result of the conversion /// is stored in result and the value returned indicates whether conversion was successful.<br/> /// </summary> /// <typeparam name="T">The type to which the <c>value</c> should be converted.</typeparam> /// <param name="typeConverter">The type converter performing the conversion (i.e. "this").</param> /// <param name="context">The usage scenario of the type conversion (path, query string, etc.)</param> /// <param name="value">The object that should be converted to <c>T</c>.</param> /// <param name="result">The result of the conversion is stored here.</param> /// <returns>True if this type converter could accomodate the conversion and False if it could not.</returns> public static bool TryConvertTo <T>(this ITypeConverter typeConverter, TypeConversionContext context, object value, out T result) { object objectResult; var converted = typeConverter.TryConvertTo(typeConverter, context, typeof(T), value, out objectResult); if (converted) { result = (T)objectResult; return(true); } else { result = default(T); return(false); } }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object obj, out object result) { if (convertTo.IsArray && obj.GetType().IsArray) { var sourceArray = (Array)obj; var destinationArray = Array.CreateInstance(convertTo.GetElementType(), sourceArray.Length); for (var i = 0; i < sourceArray.Length; i++) { var sourceElement = sourceArray.GetValue(i); object destinationElement; if (!root.TryConvertTo(root, context, convertTo.GetElementType(), sourceElement, out destinationElement)) { result = null; return false; } destinationArray.SetValue(destinationElement, i); } result = destinationArray; return true; } result = null; return false; }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object obj, out object result) { if (convertTo.IsArray && obj.GetType().IsArray) { var sourceArray = (Array)obj; var destinationArray = Array.CreateInstance(convertTo.GetElementType(), sourceArray.Length); for (var i = 0; i < sourceArray.Length; i++) { var sourceElement = sourceArray.GetValue(i); object destinationElement; if (!root.TryConvertTo(root, context, convertTo.GetElementType(), sourceElement, out destinationElement)) { result = null; return(false); } destinationArray.SetValue(destinationElement, i); } result = destinationArray; return(true); } result = null; return(false); }
public bool TryConvertTo(ITypeConverter root, TypeConversionContext context, Type convertTo, object value, out object result) { if (convertTo == typeof(string)) { result = "foo"; return true; } result = null; return false; }
/// <summary> /// Converts the specified value into the type indicated by <c>T</c>. The result of the conversion /// is stored in result and the value returned indicates whether conversion was successful.<br/> /// </summary> /// <param name="typeConverter">The type converter performing the conversion (i.e. "this").</param> /// <param name="context">The usage scenario of the type conversion (path, query string, etc.)</param> /// <param name="convertTo">The type to which the <c>value</c> should be converted.</param> /// <param name="value">The object that should be converted to <c>T</c>.</param> /// <param name="result">The result of the conversion is stored here.</param> /// <returns>True if this type converter could accomodate the conversion and False if it could not.</returns> public static bool TryConvertTo(this ITypeConverter typeConverter, TypeConversionContext context, Type convertTo, object value, out object result) { return(typeConverter.TryConvertTo(typeConverter, context, convertTo, value, out result)); }
public ContextBasedTypeConverter(TypeConversionContext context, ITypeConverter converter) { Context = context; Converter = converter; }
/// <summary> /// Converts the specified value into the type indicated by <c>T</c>. The result of the conversion /// is stored in result and the value returned indicates whether conversion was successful.<br/> /// </summary> /// <typeparam name="T">The type to which the <c>value</c> should be converted.</typeparam> /// <param name="typeConverter">The type converter performing the conversion (i.e. "this").</param> /// <param name="context">The usage scenario of the type conversion (path, query string, etc.)</param> /// <param name="value">The object that should be converted to <c>T</c>.</param> /// <returns>The <c>value</c> converted into type <c>T</c>.</returns> /// <exception cref="TypeConversionException">When the type converter could not convert the value to the specified /// type.</exception> public static T ConvertTo <T>(this ITypeConverter typeConverter, TypeConversionContext context, object value) { return((T)typeConverter.ConvertTo(context, typeof(T), value)); }
public override object VisitTypeConversion(TypeConversionContext context) { return((string)(Visit(context.typeType()))); }
public TypeConverterAttribute(Type converterType, TypeConversionContext context = TypeConversionContext.All) { ConverterType = converterType; Context = context; }