Conversion.SimpleTypes.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using System;
  2. namespace Utilities
  3. {
  4. public partial class Conversion
  5. {
  6. public static short ToInt16(object obj)
  7. {
  8. return ToInt16(obj, 0);
  9. }
  10. public static short ToInt16(object obj, short defaultValue)
  11. {
  12. short result = defaultValue;
  13. if (obj != null)
  14. {
  15. try
  16. {
  17. result = Convert.ToInt16(obj);
  18. }
  19. catch
  20. { }
  21. }
  22. return result;
  23. }
  24. public static int ToInt32(object obj)
  25. {
  26. return ToInt32(obj, 0);
  27. }
  28. public static int ToInt32(object obj, int defaultValue)
  29. {
  30. int result = defaultValue;
  31. if (obj != null)
  32. {
  33. try
  34. {
  35. result = Convert.ToInt32(obj);
  36. }
  37. catch
  38. {}
  39. }
  40. return result;
  41. }
  42. public static long ToInt64(object obj)
  43. {
  44. return ToInt64(obj, 0);
  45. }
  46. public static long ToInt64(object obj, long defaultValue)
  47. {
  48. long result = defaultValue;
  49. if (obj != null)
  50. {
  51. try
  52. {
  53. result = Convert.ToInt64(obj);
  54. }
  55. catch
  56. { }
  57. }
  58. return result;
  59. }
  60. public static ushort ToUInt16(object obj)
  61. {
  62. return ToUInt16(obj, 0);
  63. }
  64. public static ushort ToUInt16(object obj, ushort defaultValue)
  65. {
  66. ushort result = defaultValue;
  67. if (obj != null)
  68. {
  69. try
  70. {
  71. result = Convert.ToUInt16(obj);
  72. }
  73. catch
  74. { }
  75. }
  76. return result;
  77. }
  78. public static uint ToUInt32(object obj)
  79. {
  80. return ToUInt32(obj, 0);
  81. }
  82. public static uint ToUInt32(object obj, uint defaultValue)
  83. {
  84. uint result = defaultValue;
  85. if (obj != null)
  86. {
  87. try
  88. {
  89. result = Convert.ToUInt32(obj);
  90. }
  91. catch
  92. { }
  93. }
  94. return result;
  95. }
  96. public static ulong ToUInt64(object obj)
  97. {
  98. return ToUInt64(obj, 0);
  99. }
  100. public static ulong ToUInt64(object obj, ulong defaultValue)
  101. {
  102. ulong result = defaultValue;
  103. if (obj != null)
  104. {
  105. try
  106. {
  107. result = Convert.ToUInt64(obj);
  108. }
  109. catch
  110. { }
  111. }
  112. return result;
  113. }
  114. public static float ToFloat(object obj)
  115. {
  116. return ToFloat(obj, 0);
  117. }
  118. public static float ToFloat(object obj, float defaultValue)
  119. {
  120. float result = defaultValue;
  121. if (obj != null)
  122. {
  123. try
  124. {
  125. result = Convert.ToSingle(obj);
  126. }
  127. catch
  128. {}
  129. }
  130. return result;
  131. }
  132. public static double ToDouble(object obj)
  133. {
  134. return ToDouble(obj, 0);
  135. }
  136. public static double ToDouble(object obj, double defaultValue)
  137. {
  138. double result = defaultValue;
  139. if (obj != null)
  140. {
  141. try
  142. {
  143. result = Convert.ToDouble(obj);
  144. }
  145. catch
  146. { }
  147. }
  148. return result;
  149. }
  150. public static decimal ToDecimal(object obj)
  151. {
  152. return ToDecimal(obj, 0);
  153. }
  154. public static decimal ToDecimal(object obj, decimal defaultValue)
  155. {
  156. decimal result = defaultValue;
  157. if (obj != null)
  158. {
  159. try
  160. {
  161. result = Convert.ToDecimal(obj);
  162. }
  163. catch
  164. { }
  165. }
  166. return result;
  167. }
  168. public static bool ToBoolean(object obj)
  169. {
  170. return ToBoolean(obj, false);
  171. }
  172. public static bool ToBoolean(object obj, bool defaultValue)
  173. {
  174. bool result = defaultValue;
  175. if (obj != null)
  176. {
  177. try
  178. {
  179. result = Convert.ToBoolean(obj);
  180. }
  181. catch
  182. { }
  183. }
  184. return result;
  185. }
  186. public static string ToString(object obj)
  187. {
  188. string result = String.Empty;
  189. if (obj != null)
  190. {
  191. try
  192. {
  193. result = Convert.ToString(obj);
  194. }
  195. catch
  196. {}
  197. }
  198. return result;
  199. }
  200. public static char ToChar(object obj)
  201. {
  202. return ToChar(obj, new char());
  203. }
  204. public static char ToChar(object obj, char defaultValue)
  205. {
  206. char result = defaultValue;
  207. if (obj != null)
  208. {
  209. try
  210. {
  211. result = Convert.ToChar(obj);
  212. }
  213. catch
  214. { }
  215. }
  216. return result;
  217. }
  218. public static DateTime ToDateTime(object obj)
  219. {
  220. return ToDateTime(obj, DateTime.MinValue);
  221. }
  222. public static DateTime ToDateTime(object obj, DateTime defaultValue)
  223. {
  224. DateTime result = defaultValue;
  225. if (obj != null)
  226. {
  227. try
  228. {
  229. result = Convert.ToDateTime(obj);
  230. }
  231. catch
  232. { }
  233. }
  234. return result;
  235. }
  236. }
  237. }