/// <summary> /// Convert an object of any value type to the specified type using any known means /// </summary> /// <param name="value"></param> /// <param name="type"></param> /// <returns></returns> public static object Convert(object value, Type type) { object output; if (!TryConvert(value, out output, type, Objects.DefaultValue(type))) { throw new InvalidCastException("Unable to convert to type " + type.ToString()); } return(output); }
/// <summary> /// Gets the current value of the first element in the selection set, converted to the specified /// type, or if the selection set is empty, the default value for the specified type. /// </summary> /// /// <typeparam name="T"> /// The type to which the value should be converted. /// </typeparam> /// /// <returns> /// A value or object of type T. /// </returns> /// /// <url> /// http://api.jquery.com/val/#val1 /// </url> public T ValOrDefault <T>() { string val = Val(); T outVal; if (Objects.TryConvert <T>(val, out outVal)) { return(outVal); } else { return((T)Objects.DefaultValue(typeof(T))); } }
protected bool TryGetMember(string name, Type type, out object result) { object value = null; bool success = String.IsNullOrEmpty(name) ? false : InnerProperties.TryGetValue(name, out value); if (!success) { if (AllowMissingProperties) { if (type == typeof(object)) { result = MissingPropertyValue; } else { result = Objects.DefaultValue(type); } success = true; } else { throw new KeyNotFoundException("There is no property named \"" + name + "\"."); } } else { if (type == typeof(object)) { result = value; } else { result = Objects.Convert(value, type); } } return(success); }
public static bool TryConvert(object value, out object typedValue, Type type, object defaultValue = null) { typedValue = null; object output = defaultValue; bool success = false; Type realType; string stringVal = value == null ? String.Empty : value.ToString().ToLower().Trim(); if (type == typeof(string)) { typedValue = value == null ? null : value.ToString(); return(true); } else if (IsNullableType(type)) { if (stringVal == String.Empty) { typedValue = null; return(true); } realType = GetUnderlyingType(type); } else { if (stringVal == String.Empty) { typedValue = Objects.DefaultValue(type); return(false); } realType = type; } if (realType == value.GetType()) { output = value; success = true; } else if (realType == typeof(bool)) { bool result; success = TryStringToBool(stringVal, out result); if (success) { output = result; } } else if (realType.IsEnum) { output = Enum.Parse(realType, stringVal); success = true; } else if (realType == typeof(int) || realType == typeof(long) || realType == typeof(float) || realType == typeof(double) || realType == typeof(decimal)) { object val; if (TryParseNumber(stringVal, out val, realType)) { output = val; success = true; } } else if (realType == typeof(DateTime)) { DateTime val; if (DateTime.TryParse(stringVal, out val)) { output = val; success = true; } } else { output = value; } // cast the ou if (output != null && output.GetType() != realType) { if (realType is IConvertible) { try { typedValue = System.Convert.ChangeType(output, realType); success = true; } catch { typedValue = output ?? DefaultValue(realType); } } if (!success) { typedValue = output ?? DefaultValue(realType); } } else { typedValue = output ?? DefaultValue(realType); } return(success); }