public static T ConvertTo <T>(object data) { if (ValidationFun.IsNullOrEmpty(data)) { return(default(T)); } try { if (data is T) { return((T)data); } if (typeof(T).BaseType == typeof(Enum)) { return(EnumFun.GetInstance <T>(data)); } if (data is IConvertible) { return((T)Convert.ChangeType(data, typeof(T))); } return(default(T)); } catch { return(default(T)); } }
public static string GetLastChar(string text) { if (ValidationFun.IsNullOrEmpty(text)) { return(""); } return(text.Substring(text.Length - 1, 1)); }
public static int ToInt32(object data) { try { if (ValidationFun.IsNullOrEmpty(data)) { return(0); } return(Convert.ToInt32(data)); } catch { return(0); } }
public static Guid ToGuid(object data) { if (ValidationFun.IsNullOrEmpty(data)) { return(Guid.Empty); } try { return(new Guid(data.ToString())); } catch { return(Guid.Empty); } }
public static float ToFloat(object data) { try { if (ValidationFun.IsNullOrEmpty <object>(data)) { return(0f); } return(Convert.ToSingle(data)); } catch { return(0f); } }
public static double ToDouble <T>(T data, int decimals) { try { if (ValidationFun.IsNullOrEmpty <T>(data)) { return(0.0); } return(Math.Round(Convert.ToDouble(data), decimals)); } catch { return(0.0); } }
public static double ToDouble <T>(T data) { try { if (ValidationFun.IsNullOrEmpty <T>(data)) { return(0.0); } return(Convert.ToDouble(data)); } catch { return(0.0); } }
public static decimal ToDecimal(object data, int decimals) { try { if (ValidationFun.IsNullOrEmpty <object>(data)) { return(0M); } return(Math.Round(Convert.ToDecimal(data), decimals)); } catch { return(0M); } }
public static decimal ToDecimal(object data) { try { if (ValidationFun.IsNullOrEmpty(data)) { return(0M); } return(Convert.ToDecimal(data)); } catch { return(0M); } }
public static DateTime ToDateTime(object date) { try { if (ValidationFun.IsNullOrEmpty(date)) { return(Convert.ToDateTime("1900-1-1")); } return(Convert.ToDateTime(date)); } catch { return(Convert.ToDateTime("1900-1-1")); } }
public static bool ToBoolean <T>(T data) { try { if (ValidationFun.IsNullOrEmpty <T>(data)) { return(false); } return(Convert.ToBoolean(data)); } catch { return(false); } }
public static object ConvertTo(object data, Type targetType) { if (ValidationFun.IsNullOrEmpty(data)) { return(null); } try { if (data is IConvertible) { return(Convert.ChangeType(data, targetType)); } return(data); } catch { return(null); } }