private static object ChangeValueType(object from, Type type) { if (from is string strValue) { return(SSTTypeSerializer.DeserializeFromString(strValue, type)); } if (type == typeof(string)) { return(from.ToJsv()); } return(Convert.ChangeType(from, type, provider: null)); }
public static object ChangeTo(this string strValue, Type type) { if (type.IsValueType && !type.IsEnum && type.HasInterface(typeof(IConvertible))) { try { return(Convert.ChangeType(strValue, type, provider: null)); } catch (Exception ex) { Tracer.Instance.WriteError(ex); } } return(SSTTypeSerializer.DeserializeFromString(strValue, type)); }
public static MemberGetter CreateTypeConverter(Type fromType, Type toType) { if (fromType == toType) { return(null); } if (fromType == typeof(string)) { return(fromValue => SSTTypeSerializer.DeserializeFromString((string)fromValue, toType)); } if (toType == typeof(string)) { return(SSTTypeSerializer.SerializeToString); } var underlyingToType = Nullable.GetUnderlyingType(toType) ?? toType; var underlyingFromType = Nullable.GetUnderlyingType(fromType) ?? fromType; if (underlyingToType.IsEnum) { if (underlyingFromType.IsEnum || fromType == typeof(string)) { return(fromValue => Enum.Parse(underlyingToType, fromValue.ToString(), ignoreCase: true)); } if (underlyingFromType.IsIntegerType()) { return(fromValue => Enum.ToObject(underlyingToType, fromValue)); } } else if (underlyingFromType.IsEnum) { if (underlyingToType.IsIntegerType()) { return(fromValue => Convert.ChangeType(fromValue, underlyingToType, null)); } } else if (typeof(IEnumerable).IsAssignableFrom(fromType)) { return(fromValue => { var listResult = TranslateListWithElements.TryTranslateCollections( fromType, underlyingToType, fromValue); return listResult ?? fromValue; }); } else if (underlyingToType.IsValueType) { return(fromValue => Convert.ChangeType(fromValue, underlyingToType, provider: null)); } else { return(fromValue => { if (fromValue == null) { return fromValue; } var toValue = toType.CreateInstance(); toValue.PopulateWith(fromValue); return toValue; }); } return(null); }
public static object To(this string value, Type type) { return(TypeSerializer.DeserializeFromString(value, type)); }
public static T ToOrDefaultValue <T>(this string value) { return(String.IsNullOrEmpty(value) ? default(T) : TypeSerializer.DeserializeFromString <T>(value)); }
public static T To <T>(this string value) { return(TypeSerializer.DeserializeFromString <T>(value)); }