1 public static partial class Extensions 2 { 3 #region 轉換為long 4 /// <summary> 5 /// 將object轉換為long,若轉換失敗,則返回0。不丟擲異常。 6 /// </summary> 7 /// <param name="str"></param> 8 /// <returns></returns> 9 public static long ParseToLong(this object obj) 10 { 11 try 12 { 13 return long.Parse(obj.ToString()); 14 } 15 catch 16 { 17 return 0L; 18 } 19 } 20 21 /// <summary> 22 /// 將object轉換為long,若轉換失敗,則返回指定值。不丟擲異常。 23 /// </summary> 24 /// <param name="str"></param> 25 /// <param name="defaultValue"></param> 26 /// <returns></returns> 27 public static long ParseToLong(this string str, long defaultValue) 28 { 29 try 30 { 31 return long.Parse(str); 32 } 33 catch 34 { 35 return defaultValue; 36 } 37 } 38 #endregion 39 40 #region 轉換為int 41 /// <summary> 42 /// 將object轉換為int,若轉換失敗,則返回0。不丟擲異常。 43 /// </summary> 44 /// <param name="str"></param> 45 /// <returns></returns> 46 public static int ParseToInt(this object str) 47 { 48 try 49 { 50 return Convert.ToInt32(str); 51 } 52 catch 53 { 54 return 0; 55 } 56 } 57 58 /// <summary> 59 /// 將object轉換為int,若轉換失敗,則返回指定值。不丟擲異常。 60 /// null返回預設值 61 /// </summary> 62 /// <param name="str"></param> 63 /// <param name="defaultValue"></param> 64 /// <returns></returns> 65 public static int ParseToInt(this object str, int defaultValue) 66 { 67 if (str == null) 68 { 69 return defaultValue; 70 } 71 try 72 { 73 return Convert.ToInt32(str); 74 } 75 catch 76 { 77 return defaultValue; 78 } 79 } 80 #endregion 81 82 #region 轉換為short 83 /// <summary> 84 /// 將object轉換為short,若轉換失敗,則返回0。不丟擲異常。 85 /// </summary> 86 /// <param name="str"></param> 87 /// <returns></returns> 88 public static short ParseToShort(this object obj) 89 { 90 try 91 { 92 return short.Parse(obj.ToString()); 93 } 94 catch 95 { 96 return 0; 97 } 98 } 99 100 /// <summary> 101 /// 將object轉換為short,若轉換失敗,則返回指定值。不丟擲異常。 102 /// </summary> 103 /// <param name="str"></param> 104 /// <returns></returns> 105 public static short ParseToShort(this object str, short defaultValue) 106 { 107 try 108 { 109 return short.Parse(str.ToString()); 110 } 111 catch 112 { 113 return defaultValue; 114 } 115 } 116 #endregion 117 118 #region 轉換為demical 119 /// <summary> 120 /// 將object轉換為demical,若轉換失敗,則返回指定值。不丟擲異常。 121 /// </summary> 122 /// <param name="str"></param> 123 /// <returns></returns> 124 public static decimal ParseToDecimal(this object str, decimal defaultValue) 125 { 126 try 127 { 128 return decimal.Parse(str.ToString()); 129 } 130 catch 131 { 132 return defaultValue; 133 } 134 } 135 136 /// <summary> 137 /// 將object轉換為demical,若轉換失敗,則返回0。不丟擲異常。 138 /// </summary> 139 /// <param name="str"></param> 140 /// <returns></returns> 141 public static decimal ParseToDecimal(this object str) 142 { 143 try 144 { 145 return decimal.Parse(str.ToString()); 146 } 147 catch 148 { 149 return 0; 150 } 151 } 152 #endregion 153 154 #region 轉化為bool 155 /// <summary> 156 /// 將object轉換為bool,若轉換失敗,則返回false。不丟擲異常。 157 /// </summary> 158 /// <param name="str"></param> 159 /// <returns></returns> 160 public static bool ParseToBool(this object str) 161 { 162 try 163 { 164 return bool.Parse(str.ToString()); 165 } 166 catch 167 { 168 return false; 169 } 170 } 171 172 /// <summary> 173 /// 將object轉換為bool,若轉換失敗,則返回指定值。不丟擲異常。 174 /// </summary> 175 /// <param name="str"></param> 176 /// <returns></returns> 177 public static bool ParseToBool(this object str, bool result) 178 { 179 try 180 { 181 return bool.Parse(str.ToString()); 182 } 183 catch 184 { 185 return result; 186 } 187 } 188 #endregion 189 190 #region 轉換為float 191 /// <summary> 192 /// 將object轉換為float,若轉換失敗,則返回0。不丟擲異常。 193 /// </summary> 194 /// <param name="str"></param> 195 /// <returns></returns> 196 public static float ParseToFloat(this object str) 197 { 198 try 199 { 200 return float.Parse(str.ToString()); 201 } 202 catch 203 { 204 return 0; 205 } 206 } 207 208 /// <summary> 209 /// 將object轉換為float,若轉換失敗,則返回指定值。不丟擲異常。 210 /// </summary> 211 /// <param name="str"></param> 212 /// <returns></returns> 213 public static float ParseToFloat(this object str, float result) 214 { 215 try 216 { 217 return float.Parse(str.ToString()); 218 } 219 catch 220 { 221 return result; 222 } 223 } 224 #endregion 225 226 #region 轉換為Guid 227 /// <summary> 228 /// 將string轉換為Guid,若轉換失敗,則返回Guid.Empty。不丟擲異常。 229 /// </summary> 230 /// <param name="str"></param> 231 /// <returns></returns> 232 public static Guid ParseToGuid(this string str) 233 { 234 try 235 { 236 return new Guid(str); 237 } 238 catch 239 { 240 return Guid.Empty; 241 } 242 } 243 #endregion 244 245 #region 轉換為DateTime 246 /// <summary> 247 /// 將string轉換為DateTime,若轉換失敗,則返回日期最小值。不丟擲異常。 248 /// </summary> 249 /// <param name="str"></param> 250 /// <returns></returns> 251 public static DateTime ParseToDateTime(this string str) 252 { 253 try 254 { 255 if (string.IsNullOrWhiteSpace(str)) 256 { 257 return DateTime.MinValue; 258 } 259 if (str.Contains("-") || str.Contains("/")) 260 { 261 return DateTime.Parse(str); 262 } 263 else 264 { 265 int length = str.Length; 266 switch (length) 267 { 268 case 4: 269 return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture); 270 case 6: 271 return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture); 272 case 8: 273 return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); 274 case 10: 275 return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture); 276 case 12: 277 return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture); 278 case 14: 279 return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); 280 default: 281 return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); 282 } 283 } 284 } 285 catch 286 { 287 return DateTime.MinValue; 288 } 289 } 290 291 /// <summary> 292 /// 將string轉換為DateTime,若轉換失敗,則返回預設值。 293 /// </summary> 294 /// <param name="str"></param> 295 /// <param name="defaultValue"></param> 296 /// <returns></returns> 297 public static DateTime ParseToDateTime(this string str, DateTime? defaultValue) 298 { 299 try 300 { 301 if (string.IsNullOrWhiteSpace(str)) 302 { 303 return defaultValue.GetValueOrDefault(); 304 } 305 if (str.Contains("-") || str.Contains("/")) 306 { 307 return DateTime.Parse(str); 308 } 309 else 310 { 311 int length = str.Length; 312 switch (length) 313 { 314 case 4: 315 return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture); 316 case 6: 317 return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture); 318 case 8: 319 return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture); 320 case 10: 321 return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture); 322 case 12: 323 return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture); 324 case 14: 325 return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); 326 default: 327 return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture); 328 } 329 } 330 } 331 catch 332 { 333 return defaultValue.GetValueOrDefault(); 334 } 335 } 336 #endregion 337 338 #region 轉換為string 339 /// <summary> 340 /// 將object轉換為string,若轉換失敗,則返回""。不丟擲異常。 341 /// </summary> 342 /// <param name="str"></param> 343 /// <returns></returns> 344 public static string ParseToString(this object obj) 345 { 346 try 347 { 348 if (obj == null) 349 { 350 return string.Empty; 351 } 352 else 353 { 354 return obj.ToString(); 355 } 356 } 357 catch 358 { 359 return string.Empty; 360 } 361 } 362 public static string ParseToStrings<T>(this object obj) 363 { 364 try 365 { 366 var list = obj as IEnumerable<T>; 367 if (list != null) 368 { 369 return string.Join(",", list); 370 } 371 else 372 { 373 return obj.ToString(); 374 } 375 } 376 catch 377 { 378 return string.Empty; 379 } 380 381 } 382 #endregion 383 384 #region 轉換為double 385 /// <summary> 386 /// 將object轉換為double,若轉換失敗,則返回0。不丟擲異常。 387 /// </summary> 388 /// <param name="obj"></param> 389 /// <returns></returns> 390 public static double ParseToDouble(this object obj) 391 { 392 try 393 { 394 return double.Parse(obj.ToString()); 395 } 396 catch 397 { 398 return 0; 399 } 400 } 401 402 /// <summary> 403 /// 將object轉換為double,若轉換失敗,則返回指定值。不丟擲異常。 404 /// </summary> 405 /// <param name="str"></param> 406 /// <param name="defaultValue"></param> 407 /// <returns></returns> 408 public static double ParseToDouble(this object str, double defaultValue) 409 { 410 try 411 { 412 return double.Parse(str.ToString()); 413 } 414 catch 415 { 416 return defaultValue; 417 } 418 } 419 #endregion 420 421 #region 強制轉換型別 422 /// <summary> 423 /// 強制轉換型別 424 /// </summary> 425 /// <typeparam name="TResult"></typeparam> 426 /// <param name="source"></param> 427 /// <returns></returns> 428 public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source) 429 { 430 foreach (object item in source) 431 { 432 yield return (TResult)Convert.ChangeType(item, typeof(TResult)); 433 } 434 } 435 #endregion 436 }