Beispiel #1
0
 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));
     }
 }
Beispiel #2
0
 public static int GetLength(string str)
 {
     if (ValidationProvider.IsNullOrEmpty(str))
     {
         return(0);
     }
     return(Encoding.UTF8.GetByteCount(str));
 }
Beispiel #3
0
        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));
        }
Beispiel #4
0
 public static Guid ToGuid(object data)
 {
     if (ValidationProvider.IsNullOrEmpty(data))
     {
         return(Guid.Empty);
     }
     try
     {
         return(new Guid(data.ToString()));
     }
     catch
     {
         return(Guid.Empty);
     }
 }
Beispiel #5
0
 public static int ToInt32 <T>(T data)
 {
     try
     {
         if (ValidationProvider.IsNullOrEmpty <T>(data))
         {
             return(0);
         }
         return(Convert.ToInt32(data));
     }
     catch
     {
         return(0);
     }
 }
Beispiel #6
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);
     }
 }
Beispiel #7
0
 public static float ToFloat <T>(T data)
 {
     try
     {
         if (ValidationProvider.IsNullOrEmpty <T>(data))
         {
             return(0f);
         }
         return(Convert.ToSingle(data));
     }
     catch
     {
         return(0f);
     }
 }
Beispiel #8
0
 public static double ToDouble <T>(T data)
 {
     try
     {
         if (ValidationProvider.IsNullOrEmpty <T>(data))
         {
             return(0.0);
         }
         return(Convert.ToDouble(data));
     }
     catch
     {
         return(0.0);
     }
 }
Beispiel #9
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);
     }
 }
Beispiel #10
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);
     }
 }
Beispiel #11
0
 public static decimal ToDecimal(object data)
 {
     try
     {
         if (ValidationProvider.IsNullOrEmpty(data))
         {
             return(0M);
         }
         return(Convert.ToDecimal(data));
     }
     catch
     {
         return(0M);
     }
 }
Beispiel #12
0
 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"));
     }
 }
Beispiel #13
0
 public static bool ToBoolean <T>(T data)
 {
     try
     {
         if (ValidationProvider.IsNullOrEmpty <T>(data))
         {
             return(false);
         }
         return(Convert.ToBoolean(data));
     }
     catch
     {
         return(false);
     }
 }
Beispiel #14
0
 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);
     }
 }