123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Extensions
- {
- public static partial class Extensions
- {
- #region 转换为long
- /// <summary>
- /// 将object转换为long,若转换失败,则返回0。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static long ParseToLong(this object obj)
- {
- try
- {
- return long.Parse(obj.ToString());
- }
- catch
- {
- return 0L;
- }
- }
- /// <summary>
- /// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static long ParseToLong(this string str, long defaultValue)
- {
- try
- {
- return long.Parse(str);
- }
- catch
- {
- return defaultValue;
- }
- }
- #endregion
- #region 转换为int
- /// <summary>
- /// 将object转换为int,若转换失败,则返回0。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static int ParseToInt(this object str)
- {
- try
- {
- return Convert.ToInt32(str);
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 将object转换为int,若转换失败,则返回指定值。不抛出异常。
- /// null返回默认值
- /// </summary>
- /// <param name="str"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static int ParseToInt(this object str, int defaultValue)
- {
- if (str == null)
- {
- return defaultValue;
- }
- try
- {
- return Convert.ToInt32(str);
- }
- catch
- {
- return defaultValue;
- }
- }
- #endregion
- #region 转换为short
- /// <summary>
- /// 将object转换为short,若转换失败,则返回0。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static short ParseToShort(this object obj)
- {
- try
- {
- return short.Parse(obj.ToString());
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 将object转换为short,若转换失败,则返回指定值。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static short ParseToShort(this object str, short defaultValue)
- {
- try
- {
- return short.Parse(str.ToString());
- }
- catch
- {
- return defaultValue;
- }
- }
- #endregion
- #region 转换为demical
- /// <summary>
- /// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static decimal ParseToDecimal(this object str, decimal defaultValue)
- {
- try
- {
- return decimal.Parse(str.ToString());
- }
- catch
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// 将object转换为demical,若转换失败,则返回0。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static decimal ParseToDecimal(this object str)
- {
- try
- {
- return decimal.Parse(str.ToString());
- }
- catch
- {
- return 0;
- }
- }
- #endregion
- #region 转化为bool
- /// <summary>
- /// 将object转换为bool,若转换失败,则返回false。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool ParseToBool(this object str)
- {
- try
- {
- return bool.Parse(str.ToString());
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool ParseToBool(this object str, bool result)
- {
- try
- {
- return bool.Parse(str.ToString());
- }
- catch
- {
- return result;
- }
- }
- #endregion
- #region 转换为float
- /// <summary>
- /// 将object转换为float,若转换失败,则返回0。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static float ParseToFloat(this object str)
- {
- try
- {
- return float.Parse(str.ToString());
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 将object转换为float,若转换失败,则返回指定值。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static float ParseToFloat(this object str, float result)
- {
- try
- {
- return float.Parse(str.ToString());
- }
- catch
- {
- return result;
- }
- }
- #endregion
- #region 转换为Guid
- /// <summary>
- /// 将string转换为Guid,若转换失败,则返回Guid.Empty。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static Guid ParseToGuid(this string str)
- {
- try
- {
- return new Guid(str);
- }
- catch
- {
- return Guid.Empty;
- }
- }
- #endregion
- #region 转换为DateTime
- /// <summary>
- /// 将string转换为DateTime,若转换失败,则返回日期最小值。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static DateTime ParseToDateTime(this string str)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(str))
- {
- return DateTime.MinValue;
- }
- if (str.Contains("-") || str.Contains("/"))
- {
- return DateTime.Parse(str);
- }
- else
- {
- int length = str.Length;
- switch (length)
- {
- case 4:
- return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
- case 6:
- return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
- case 8:
- return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
- case 10:
- return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
- case 12:
- return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
- case 14:
- return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
- default:
- return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
- }
- }
- }
- catch
- {
- return DateTime.MinValue;
- }
- }
- /// <summary>
- /// 将string转换为DateTime,若转换失败,则返回默认值。
- /// </summary>
- /// <param name="str"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
- {
- try
- {
- if (string.IsNullOrWhiteSpace(str))
- {
- return defaultValue.GetValueOrDefault();
- }
- if (str.Contains("-") || str.Contains("/"))
- {
- return DateTime.Parse(str);
- }
- else
- {
- int length = str.Length;
- switch (length)
- {
- case 4:
- return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
- case 6:
- return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
- case 8:
- return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
- case 10:
- return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
- case 12:
- return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
- case 14:
- return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
- default:
- return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
- }
- }
- }
- catch
- {
- return defaultValue.GetValueOrDefault();
- }
- }
- #endregion
- #region 转换为string
- /// <summary>
- /// 将object转换为string,若转换失败,则返回""。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string ParseToString(this object obj)
- {
- try
- {
- if (obj == null)
- {
- return string.Empty;
- }
- else
- {
- return obj.ToString();
- }
- }
- catch
- {
- return string.Empty;
- }
- }
- public static string ParseToStrings<T>(this object obj)
- {
- try
- {
- var list = obj as IEnumerable<T>;
- if (list != null)
- {
- return string.Join(",", list);
- }
- else
- {
- return obj.ToString();
- }
- }
- catch
- {
- return string.Empty;
- }
- }
- #endregion
- #region 转换为double
- /// <summary>
- /// 将object转换为double,若转换失败,则返回0。不抛出异常。
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static double ParseToDouble(this object obj)
- {
- try
- {
- return double.Parse(obj.ToString());
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 将object转换为double,若转换失败,则返回指定值。不抛出异常。
- /// </summary>
- /// <param name="str"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static double ParseToDouble(this object str, double defaultValue)
- {
- try
- {
- return double.Parse(str.ToString());
- }
- catch
- {
- return defaultValue;
- }
- }
- #endregion
- #region 强制转换类型
- /// <summary>
- /// 强制转换类型
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="source"></param>
- /// <returns></returns>
- public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
- {
- foreach (object item in source)
- {
- yield return (TResult)Convert.ChangeType(item, typeof(TResult));
- }
- }
- #endregion
- }
- }
|