123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System;
- using System.Text.RegularExpressions;
- namespace Common
- {
- public class TypeConverter
- {
- public static bool StrToBool(object expression, bool defValue)
- {
- if (expression != null)
- {
- return TypeConverter.StrToBool(expression, defValue);
- }
- return defValue;
- }
- public static bool StrToBool(string expression, bool defValue)
- {
- if (expression != null)
- {
- if (string.Compare(expression, "true", true) == 0)
- {
- return true;
- }
- if (string.Compare(expression, "false", true) == 0)
- {
- return false;
- }
- }
- return defValue;
- }
- public static int ObjectToInt(object expression)
- {
- return TypeConverter.ObjectToInt(expression, 0);
- }
- public static int ObjectToInt(object expression, int defValue)
- {
- if (expression != null)
- {
- return TypeConverter.StrToInt(expression.ToString(), defValue);
- }
- return defValue;
- }
- public static int StrToInt(string str)
- {
- return TypeConverter.StrToInt(str, 0);
- }
- public static int StrToInt(string str, int defValue)
- {
- if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), "^([-]|[0-9])[0-9]*(\\.\\w*)?$"))
- {
- return defValue;
- }
- int result;
- if (int.TryParse(str, out result))
- {
- return result;
- }
- return Convert.ToInt32(TypeConverter.StrToFloat(str, (float)defValue));
- }
- public static float StrToFloat(object strValue, float defValue)
- {
- if (strValue == null)
- {
- return defValue;
- }
- return TypeConverter.StrToFloat(strValue.ToString(), defValue);
- }
- public static float ObjectToFloat(object strValue, float defValue)
- {
- if (strValue == null)
- {
- return defValue;
- }
- return TypeConverter.StrToFloat(strValue.ToString(), defValue);
- }
- public static float ObjectToFloat(object strValue)
- {
- return TypeConverter.ObjectToFloat(strValue.ToString(), 0f);
- }
- public static float StrToFloat(string strValue)
- {
- if (strValue == null)
- {
- return 0f;
- }
- return TypeConverter.StrToFloat(strValue.ToString(), 0f);
- }
- public static float StrToFloat(string strValue, float defValue)
- {
- if (strValue == null || strValue.Length > 10)
- {
- return defValue;
- }
- float result = defValue;
- if (strValue != null)
- {
- bool flag = Regex.IsMatch(strValue, "^([-]|[0-9])[0-9]*(\\.\\w*)?$");
- if (flag)
- {
- float.TryParse(strValue, out result);
- }
- }
- return result;
- }
- public static DateTime StrToDateTime(string str, DateTime defValue)
- {
- DateTime result;
- if (!string.IsNullOrEmpty(str) && DateTime.TryParse(str, out result))
- {
- return result;
- }
- return defValue;
- }
- public static DateTime StrToDateTime(string str)
- {
- return TypeConverter.StrToDateTime(str, DateTime.Now);
- }
- public static DateTime ObjectToDateTime(object obj)
- {
- return TypeConverter.StrToDateTime(obj.ToString());
- }
- public static DateTime ObjectToDateTime(object obj, DateTime defValue)
- {
- return TypeConverter.StrToDateTime(obj.ToString(), defValue);
- }
- }
- }
|