1 using Newtonsoft.Json;
2 using System;
3 using System.Collections.Generic;
4 using System.Collections.Specialized;
5 using System.ComponentModel;
6 using System.Data;
7 using System.IO;
8 using System.Reflection;
9 using System.Text.RegularExpressions;
10
11 namespace Common
12 {
13 public class ConverHelper
14 {
15 #region object 转换T类型数据
16 ///
17 /// 转换 object为 T 类型
18 ///
19 /// T 类型
20 /// object 数据
21 /// 默认值
22 /// 是否抛出异常
23 ///
24 public static T ToTSource(object obj, T defVal = default(T), bool exp = false)
25 {
26 T result = defVal;
27 if (obj == null)
28 {
29 return result;
30 }
31 if (obj is T)
32 {
33 return (T)obj;
34 }
35
36 try
37 {
38 Type conversionType = typeof(T);
39 object obj2 = null;
40 if (conversionType.Equals(typeof(Guid)))
41 obj2 = new Guid(Convert.ToString(obj));
42 else
43 obj2 = Convert.ChangeType(obj, conversionType);
44 result = (T)obj2;
45 }
46 catch (Exception ex)
47 {
48 if (exp == true)
49 {
50 throw ex;
51 }
52 }
53 return result;
54 }
55 #endregion
56
57 #region 数值类型转换
58 #region 转换为 bool 类型
59 ///
60 /// 将object转换为 bool 类型
61 ///
62 /// 待转换的object
63 /// 缺省值(转换不成功)
64 /// 是否抛出异常(默认不抛出)
65 /// 转换后的bool类型结果
66 public static bool ToBool(object obj, bool defVal = false, bool exp = false)
67 {
68 bool result = defVal;
69 try
70 {
71 if (obj != null)
72 result = Convert.ToBoolean(obj);
73 }
74 catch (Exception ex)
75 {
76 if (exp == true)
77 {
78 throw ex;
79 }
80 }
81 return result;
82 }
83
84 #endregion
85
86 #region 转换为 Int 数值类型
87 ///
88 /// 将object转换Int 类型
89 ///
90 /// 待转换的object
91 /// 缺省值(转换不成功)
92 /// 是否抛出异常(默认不抛出)
93 /// 转换后的Int类型结果
94 public static int ToInt(object obj, int defVal = 0, bool exp = false)
95 {
96 int result = defVal;
97 try
98 {
99 if (obj != null)
100 result = Convert.ToInt32(obj);
101 }
102 catch (Exception ex)
103 {
104 if (exp == true)
105 {
106 throw ex;
107 }
108 }
109 return result;
110 }
111
112 #endregion
113
114 #region 转换为 Float 数值类型
115 ///
116 /// 将object转换 Float 类型
117 ///
118 /// 待转换的object
119 /// 缺省值(转换不成功)
120 /// 是否抛出异常(默认不抛出)
121 /// 转换后的Int类型结果
122 public static float ToFloat(object obj, float defVal = 0, bool exp = false)
123 {
124 float result = defVal;
125 try
126 {
127 if (obj != null)
128 result = Convert.ToSingle(obj);
129 }
130 catch (Exception ex)
131 {
132 if (exp == true)
133 {
134 throw ex;
135 }
136 }
137 return result;
138 }
139
140 #endregion
141
142 #region 转换为 Double 数值类型
143 ///
144 /// 将object转换 double 类型
145 ///
146 /// 待转换的object
147 /// 缺省值(转换不成功)
148 /// 是否抛出异常(默认不抛出)
149 /// 转换后的Int类型结果
150 public static double ToDouble(object obj, double defVal = 0, bool exp = false)
151 {
152 double result = defVal;
153 try
154 {
155 if (obj != null)
156 result = Convert.ToDouble(obj);
157 }
158 catch (Exception ex)
159 {
160 if (exp == true)
161 {
162 throw ex;
163 }
164 }
165 return result;
166 }
167
168 #endregion
169
170 #region 转换为 decimal 数值类型
171 ///
172 /// 将对象转换 decimal 类型
173 ///
174 /// 待转换的字符串
175 /// 缺省值(转换不成功)
176 /// 是否抛出异常(默认不抛出)
177 /// 转换后的decimal类型结果
178 public static decimal ToDecimal(object obj, decimal defVal = 0m, bool exp = false)
179 {
180 decimal result = defVal;
181 try
182 {
183 if (obj != null)
184 result = Convert.ToDecimal(obj);
185 }
186 catch (Exception ex)
187 {
188 if (exp == true)
189 {
190 throw ex;
191 }
192 }
193 return result;
194 }
195 #endregion
196 #endregion
197
198 #region 转换为 DateTime 日期类型
199 ///
200 /// 将object转换 DateTime 日期类型
201 ///
202 /// 待转换的字符串
203 /// 缺省值(转换不成功)
204 /// 是否抛出异常(默认不抛出)
205 /// 转换后的DateTime类型结果
206 public static DateTime ToDateTime(object obj, string defVal = "1970-01-01 08:00:00", bool exp = false)
207 {
208 DateTime result = DateTime.Parse(defVal);
209 try
210 {
211 if (obj != null)
212 result = Convert.ToDateTime(obj);
213 }
214 catch (Exception ex)
215 {
216 if (exp == true)
217 {
218 throw ex;
219 }
220 }
221 return result;
222 }
223
224 #endregion
225
226 /// 将数据转化为 type 类型
227 ///
228 /// 要转化的值
229 /// 目标类型
230 /// 转化为目标类型的 Object 对象
231 private static object ChangeType(object value, Type type)
232 {
233 if (type.IsGenericType && type.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
234 {
235 NullableConverter convertor = new NullableConverter(type);
236 return Convert.IsDBNull(value) ? null : convertor.ConvertFrom(value);
237 }
238 return Convert.ChangeType(Convert.IsDBNull(value) ? null : value, type);
239 }
240
241 #endregion
242
243 #region 杂项转换
244 ///
245 /// 获取默认值
246 ///
247 ///
248 ///
249 private static object GetNullInternal(Type type)
250 {
251 if (type.IsValueType)
252 {
253 if (type.IsEnum)
254 {
255 return GetNullInternal(Enum.GetUnderlyingType(type));
256 }
257 if (type.IsPrimitive)
258 {
259 if (type == typeof(int))
260 {
261 return 0;
262 }
263 if (type == typeof(double))
264 {
265 return 0.0;
266 }
267 if (type == typeof(short))
268 {
269 return (short)0;
270 }
271 if (type == typeof(sbyte))
272 {
273 return (sbyte)0;
274 }
275 if (type == typeof(long))
276 {
277 return 0L;
278 }
279 if (type == typeof(byte))
280 {
281 return (byte)0;
282 }
283 if (type == typeof(ushort))
284 {
285 return (ushort)0;
286 }
287 if (type == typeof(uint))
288 {
289 return 0;
290 }
291 if (type == typeof(ulong))
292 {
293 return (ulong)0L;
294 }
295 if (type == typeof(ulong))
296 {
297 return (ulong)0L;
298 }
299 if (type == typeof(float))
300 {
301 return 0f;
302 }
303 if (type == typeof(bool))
304 {
305 return false;
306 }
307 if (type == typeof(char))
308 {
309 return '\0';
310 }
311 }
312 else
313 {
314 if (type == typeof(DateTime))
315 {
316 return DateTime.MinValue;
317 }
318 if (type == typeof(decimal))
319 {
320 return 0M;
321 }
322 if (type == typeof(Guid))
323 {
324 return Guid.Empty;
325 }
326 if (type == typeof(TimeSpan))
327 {
328 return new TimeSpan(0, 0, 0);
329 }
330 }
331 }
332 return null;
333 }
334 #endregion
335 #endregion
336
337 #region 转换字符串
338 ///
339 /// 转换字符串,因为可能有空,直接转换会报错
340 ///
341 ///
342 ///
343 ///
344 public static string ToString(object obj, string defVal = "")
345 {
346 string result = defVal;
347 if (obj == null)
348 {
349 return result;
350 }
351 return obj.ToString();
352 }
353 #endregion
354 }
355 }