123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- namespace Common
- {
- /// <summary>
- /// 数据类型转换扩展方法
- /// </summary>
- public static class ConvertExtension
- {
- #region 类型转换
- /// <summary>
- /// object 转换成string 包括为空的情况
- /// </summary>
- /// <param name="obj"></param>
- /// <returns>返回值不含空格</returns>
- public static string ToStringEx(this object obj)
- {
- return obj == null ? string.Empty : obj.ToString().Trim();
- }
- /// <summary>
- /// 时间object 转换成格式化的string 包括为空的情况
- /// </summary>
- /// <param name="obj"></param>
- /// <param name="format"></param>
- /// <returns>返回值不含空格</returns>
- public static string TryToDateTimeToString(this object obj, string format)
- {
- if (obj == null)
- return string.Empty;
- DateTime dt;
- if (DateTime.TryParse(obj.ToString(), out dt))
- return dt.ToString(format);
- else
- return string.Empty;
- }
- /// <summary>
- /// 字符转Int
- /// </summary>
- /// <param name="obj"></param>
- /// <returns>成功:返回对应Int值;失败:返回0</returns>
- public static int TryToInt32(this object obj)
- {
- int rel = 0;
- if (!string.IsNullOrEmpty(obj.ToStringEx()))
- {
- int.TryParse(obj.ToStringEx(), out rel);
- }
- return rel;
- }
- /// <summary>
- /// 字符转Int64
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static Int64 TryToInt64(this object obj)
- {
- Int64 rel = 0;
- if (!string.IsNullOrEmpty(obj.ToStringEx()))
- {
- Int64.TryParse(obj.ToStringEx(), out rel);
- }
- return rel;
- }
- /// <summary>
- /// 字符转DateTime
- /// </summary>
- /// <param name="obj"></param>
- /// <returns>成功:返回对应Int值;失败:时间初始值</returns>
- public static DateTime TryToDateTime(this object obj)
- {
- DateTime rel = new DateTime();
- if (!string.IsNullOrEmpty(obj.ToStringEx()))
- {
- DateTime.TryParse(obj.ToStringEx(), out rel);
- }
- return rel;
- }
- /// <summary>
- /// 转换成bool类型
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static Boolean TryToBoolean(this object obj)
- {
- Boolean rel = false;
- if (!string.IsNullOrEmpty(obj.ToStringEx()))
- {
- string s = obj.ToStringEx();
- if (s.Equals("true") || s.Equals("1") || s.Equals("是"))
- {
- rel = true;
- }
- else
- {
- Boolean.TryParse(obj.ToStringEx(), out rel);
- }
- }
- return rel;
- }
- #endregion
- #region 补足位数
- /// <summary>
- /// 指定字符串的固定长度,如果字符串小于固定长度,
- /// 则在字符串的前面补足零,可设置的固定长度最大为9位
- /// </summary>
- /// <param name="text">原始字符串</param>
- /// <param name="limitedLength">字符串的固定长度</param>
- public static string RepairZero(this string text, int limitedLength)
- {
- //补足0的字符串
- string temp = "";
- //补足0
- for (int i = 0; i < limitedLength - text.Length; i++)
- {
- temp += "0";
- }
- //连接text
- temp += text;
- //返回补足0的字符串
- return temp;
- }
- /// <summary>
- /// 小时、分钟、秒小于10补足0
- /// </summary>
- /// <param name="text">原始字符串</param>
- /// <returns></returns>
- public static string RepairZero(this int text)
- {
- string res = string.Empty;
- if (text >= 0 && text < 10)
- {
- res += "0" + text;
- }
- else
- {
- res = text.ToString();
- }
- return res;
- }
- #endregion
- #region 把对象类型转换成指定的类型,转化失败时返回指定默认值
- /// <summary>
- /// 把对象类型转换成指定的类型,转化失败时返回指定默认值
- /// </summary>
- /// <typeparam name="T">动态类型</typeparam>
- /// <param name="value">要转换的原对象</param>
- /// <param name="detaultValue">转换失败时返回的默认值</param>
- /// <returns>转化后指定类型对象,转化失败时返回指定默认值</returns>
- public static T CastTo<T>(this object value, T detaultValue)
- {
- object result;
- Type t = typeof(T);
- try
- {
- result = t.IsEnum ? System.Enum.Parse(t, value.ToString()) : Convert.ChangeType(value, t);
- }
- catch (Exception)
- {
- return detaultValue;
- }
- return (T)result;
- }
- #endregion
- #region 把对象类型转换成指定的类型,转化失败时返回类型默认值
- /// <summary>
- /// 把对象类型转换成指定的类型,转化失败时返回类型默认值
- /// </summary>
- /// <typeparam name="T">动态类型</typeparam>
- /// <param name="value">要转换的原对象</param>
- /// <returns>转化后指定类型对象,转化失败时返回类型默认值</returns>
- public static T CastTo<T>(this object value)
- {
- object result;
- Type t = typeof(T);
- try
- {
- if (t.IsEnum)
- {
- result = System.Enum.Parse(t, value.ToString());
- }
- else if (t == typeof(Guid))
- {
- result = Guid.Parse(value.ToString());
- }
- else
- {
- result = Convert.ChangeType(value, t);
- }
- }
- catch (Exception)
- {
- result = default(T);
- }
- return (T)result;
- }
- #endregion
- #region DataTable转换成List集合
- /// <summary>
- /// DataTable转换成List集合
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="dt"></param>
- /// <returns></returns>
- public static List<T> DataTableToList<T>(this DataTable dt) where T : new()
- {
- // 定义集合
- IList<T> ts = new List<T>();
- // 获得此模型的类型
- Type type = typeof(T);
- foreach (DataRow dr in dt.Rows)
- {
- T t = new T();
- // 获得此模型的公共属性
- PropertyInfo[] propertys = t.GetType().GetProperties();
- foreach (PropertyInfo pi in propertys)
- {
- string tempName = pi.Name;
- if (dt.Columns.Contains(tempName))
- {
- // 判断此属性是否有Setter
- if (!pi.CanWrite) continue;
- object value = dr[tempName];
- if (value != DBNull.Value)
- pi.SetValue(t, value, null);
- }
- }
- ts.Add(t);
- }
- return ts.CastTo<List<T>>();
- }
- #endregion
- }
- }
|