TypeConverter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace Common
  4. {
  5. public class TypeConverter
  6. {
  7. public static bool StrToBool(object expression, bool defValue)
  8. {
  9. if (expression != null)
  10. {
  11. return TypeConverter.StrToBool(expression, defValue);
  12. }
  13. return defValue;
  14. }
  15. public static bool StrToBool(string expression, bool defValue)
  16. {
  17. if (expression != null)
  18. {
  19. if (string.Compare(expression, "true", true) == 0)
  20. {
  21. return true;
  22. }
  23. if (string.Compare(expression, "false", true) == 0)
  24. {
  25. return false;
  26. }
  27. }
  28. return defValue;
  29. }
  30. public static int ObjectToInt(object expression)
  31. {
  32. return TypeConverter.ObjectToInt(expression, 0);
  33. }
  34. public static int ObjectToInt(object expression, int defValue)
  35. {
  36. if (expression != null)
  37. {
  38. return TypeConverter.StrToInt(expression.ToString(), defValue);
  39. }
  40. return defValue;
  41. }
  42. public static int StrToInt(string str)
  43. {
  44. return TypeConverter.StrToInt(str, 0);
  45. }
  46. public static int StrToInt(string str, int defValue)
  47. {
  48. if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), "^([-]|[0-9])[0-9]*(\\.\\w*)?$"))
  49. {
  50. return defValue;
  51. }
  52. int result;
  53. if (int.TryParse(str, out result))
  54. {
  55. return result;
  56. }
  57. return Convert.ToInt32(TypeConverter.StrToFloat(str, (float)defValue));
  58. }
  59. public static float StrToFloat(object strValue, float defValue)
  60. {
  61. if (strValue == null)
  62. {
  63. return defValue;
  64. }
  65. return TypeConverter.StrToFloat(strValue.ToString(), defValue);
  66. }
  67. public static float ObjectToFloat(object strValue, float defValue)
  68. {
  69. if (strValue == null)
  70. {
  71. return defValue;
  72. }
  73. return TypeConverter.StrToFloat(strValue.ToString(), defValue);
  74. }
  75. public static float ObjectToFloat(object strValue)
  76. {
  77. return TypeConverter.ObjectToFloat(strValue.ToString(), 0f);
  78. }
  79. public static float StrToFloat(string strValue)
  80. {
  81. if (strValue == null)
  82. {
  83. return 0f;
  84. }
  85. return TypeConverter.StrToFloat(strValue.ToString(), 0f);
  86. }
  87. public static float StrToFloat(string strValue, float defValue)
  88. {
  89. if (strValue == null || strValue.Length > 10)
  90. {
  91. return defValue;
  92. }
  93. float result = defValue;
  94. if (strValue != null)
  95. {
  96. bool flag = Regex.IsMatch(strValue, "^([-]|[0-9])[0-9]*(\\.\\w*)?$");
  97. if (flag)
  98. {
  99. float.TryParse(strValue, out result);
  100. }
  101. }
  102. return result;
  103. }
  104. public static DateTime StrToDateTime(string str, DateTime defValue)
  105. {
  106. DateTime result;
  107. if (!string.IsNullOrEmpty(str) && DateTime.TryParse(str, out result))
  108. {
  109. return result;
  110. }
  111. return defValue;
  112. }
  113. public static DateTime StrToDateTime(string str)
  114. {
  115. return TypeConverter.StrToDateTime(str, DateTime.Now);
  116. }
  117. public static DateTime ObjectToDateTime(object obj)
  118. {
  119. return TypeConverter.StrToDateTime(obj.ToString());
  120. }
  121. public static DateTime ObjectToDateTime(object obj, DateTime defValue)
  122. {
  123. return TypeConverter.StrToDateTime(obj.ToString(), defValue);
  124. }
  125. }
  126. }