public static T ConvertTo <T>(object data) { if (ValidationProvider.IsNullOrEmpty(data)) { return(default(T)); } try { if (data is T) { return((T)data); } if (typeof(T).BaseType == typeof(Enum)) { return(EnumProvider.GetInstance <T>(data)); } if (data is IConvertible) { return((T)Convert.ChangeType(data, typeof(T))); } return(default(T)); } catch { return(default(T)); } }
public static int GetLength(string str) { if (ValidationProvider.IsNullOrEmpty(str)) { return(0); } return(Encoding.UTF8.GetByteCount(str)); }
public static bool IsIPLegality(string ip) { if (ValidationProvider.IsNullOrEmpty(ip)) { return(false); } ip = ip.Trim(); string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"; return(RegexProvider.IsMatch(ip, pattern)); }
public static Guid ToGuid(object data) { if (ValidationProvider.IsNullOrEmpty(data)) { return(Guid.Empty); } try { return(new Guid(data.ToString())); } catch { return(Guid.Empty); } }
public static int ToInt32 <T>(T data) { try { if (ValidationProvider.IsNullOrEmpty <T>(data)) { return(0); } return(Convert.ToInt32(data)); } catch { return(0); } }
public static double ToDouble <T>(T data, int decimals, MidpointRounding pointRound) { try { if (ValidationProvider.IsNullOrEmpty <T>(data)) { return(0.0); } return(Math.Round(Convert.ToDouble(data), decimals, pointRound)); } catch { return(0.0); } }
public static float ToFloat <T>(T data) { try { if (ValidationProvider.IsNullOrEmpty <T>(data)) { return(0f); } return(Convert.ToSingle(data)); } catch { return(0f); } }
public static double ToDouble <T>(T data) { try { if (ValidationProvider.IsNullOrEmpty <T>(data)) { return(0.0); } return(Convert.ToDouble(data)); } catch { return(0.0); } }
public static double ToDouble(object data, int decimals) { try { if (ValidationProvider.IsNullOrEmpty <object>(data)) { return(0.0); } return(Math.Round(Convert.ToDouble(data), decimals)); } catch { return(0.0); } }
public static decimal ToDecimal <T>(T data, int decimals) { try { if (ValidationProvider.IsNullOrEmpty <T>(data)) { return(0M); } return(Math.Round(Convert.ToDecimal(data), decimals)); } catch { return(0M); } }
public static decimal ToDecimal(object data) { try { if (ValidationProvider.IsNullOrEmpty(data)) { return(0M); } return(Convert.ToDecimal(data)); } catch { return(0M); } }
public static DateTime ToDateTime(object date) { try { if (ValidationProvider.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 (ValidationProvider.IsNullOrEmpty <T>(data)) { return(false); } return(Convert.ToBoolean(data)); } catch { return(false); } }
public static object ConvertTo(object data, Type targetType) { if (ValidationProvider.IsNullOrEmpty(data)) { return(null); } try { if (data is IConvertible) { return(Convert.ChangeType(data, targetType)); } return(data); } catch { return(null); } }