Exemple #1
0
 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));
 }
Exemple #3
0
 public static int ToInt32(object data)
 {
     try
     {
         if (ValidationFun.IsNullOrEmpty(data))
         {
             return(0);
         }
         return(Convert.ToInt32(data));
     }
     catch
     {
         return(0);
     }
 }
Exemple #4
0
 public static Guid ToGuid(object data)
 {
     if (ValidationFun.IsNullOrEmpty(data))
     {
         return(Guid.Empty);
     }
     try
     {
         return(new Guid(data.ToString()));
     }
     catch
     {
         return(Guid.Empty);
     }
 }
Exemple #5
0
 public static float ToFloat(object data)
 {
     try
     {
         if (ValidationFun.IsNullOrEmpty <object>(data))
         {
             return(0f);
         }
         return(Convert.ToSingle(data));
     }
     catch
     {
         return(0f);
     }
 }
Exemple #6
0
 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);
     }
 }
Exemple #7
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);
     }
 }
Exemple #8
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);
     }
 }
Exemple #9
0
 public static decimal ToDecimal(object data)
 {
     try
     {
         if (ValidationFun.IsNullOrEmpty(data))
         {
             return(0M);
         }
         return(Convert.ToDecimal(data));
     }
     catch
     {
         return(0M);
     }
 }
Exemple #10
0
 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"));
     }
 }
Exemple #11
0
 public static bool ToBoolean <T>(T data)
 {
     try
     {
         if (ValidationFun.IsNullOrEmpty <T>(data))
         {
             return(false);
         }
         return(Convert.ToBoolean(data));
     }
     catch
     {
         return(false);
     }
 }
Exemple #12
0
 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);
     }
 }