#region 转换为string
/// <summary>
/// 将object转换为string,若转换失败,则返回""。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string ParseToString(this object obj)
{
return Convert.ToString(obj) ?? string.Empty;
}
#endregion
#region 转换为long
/// <summary>
/// 将object转换为long,若转换失败,则返回0。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static long ParseToLong(this object obj)
{
if (obj == null)
{
return 0;
}
else
{
bool boolean = long.TryParse(obj.ToString() ?? string.Empty, out long result);
return result;
}
}
/// <summary>
/// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long ParseToLong(this object obj, long defaultValue)
{
if (obj == null)
{
return defaultValue;
}
else
{
bool boolean = long.TryParse(obj.ToString() ?? string.Empty, out long result);
if (boolean)
return result;
else
return defaultValue;
}
}
#endregion
#region 转换为int
/// <summary>
/// 将object转换为int,若转换失败,则返回0。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static int ParseToInt(this object obj)
{
if (obj == null)
{
return 0;
}
else
{
bool boolean = int.TryParse(obj.ToString() ?? string.Empty, out int result);
return result;
}
}
/// <summary>
/// 将object转换为int,若转换失败,则返回指定值。不抛出异常。
/// null返回默认值
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int ParseToInt(this object obj, int defaultValue)
{
if (obj == null)
{
return defaultValue;
}
else
{
bool boolean = int.TryParse(obj.ToString() ?? string.Empty, out int result);
if (boolean)
return result;
else
return defaultValue;
}
}
#endregion
#region 转换为double
/// <summary>
/// 将object转换为double,若转换失败,则返回0。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static double ParseToDouble(this object obj)
{
if (obj == null)
{
return 0;
}
else
{
bool boolean = double.TryParse(obj.ToString() ?? string.Empty, out double result);
return result;
}
}
/// <summary>
/// 将object转换为double,若转换失败,则返回指定值。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double ParseToDouble(this object obj, double defaultValue)
{
if (obj == null)
{
return defaultValue;
}
else
{
bool boolean = double.TryParse(obj.ToString() ?? string.Empty, out double result);
if (boolean)
return result;
else
return defaultValue;
}
}
#endregion
#region 转换为demical
/// <summary>
/// 将object转换为demical,若转换失败,则返回0。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static decimal ParseToDecimal(this object obj)
{
if (obj == null)
{
return 0;
}
else
{
bool boolean = decimal.TryParse(obj.ToString() ?? string.Empty, out decimal result);
return result;
}
}
/// <summary>
/// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static decimal ParseToDecimal(this object obj, decimal defaultValue)
{
if (obj == null)
{
return defaultValue;
}
else
{
bool boolean = decimal.TryParse(obj.ToString() ?? string.Empty, out decimal result);
if (boolean)
return result;
else
return defaultValue;
}
}
#endregion
#region 转化为bool
/// <summary>
/// 将object转换为bool,若转换失败,则返回false。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static bool ParseToBool(this object obj)
{
if (obj == null)
{
return false;
}
else
{
bool boolean = bool.TryParse(obj.ToString() ?? string.Empty, out bool result);
return result;
}
}
/// <summary>
/// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool ParseToBool(this object obj, bool defaultValue)
{
if (obj == null)
{
return defaultValue;
}
else
{
bool boolean = bool.TryParse(obj.ToString() ?? string.Empty, out bool result);
if (boolean)
return result;
else
return defaultValue;
}
}
#endregion
#region 时间与时间戳转换
/// <summary>
/// 时间转换为毫秒时间戳
/// </summary>
/// <param name="nowTime"></param>
/// <returns>时间戳(毫秒)</returns>
public static long ParseToUnixTime(this DateTime nowTime)
{
var startTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return (long)Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero);
}
#endregion
#region 删除最后一个字符之后的字符
/// <summary>
/// 删除最后结尾的一个逗号
/// </summary>
public static string DelLastComma(string str)
{
return str.Substring(0, str.LastIndexOf(","));
}
/// <summary>
/// 删除最后结尾的指定字符后的字符
/// </summary>
public static string DelLastChar(string str, string strchar)
{
return str.Substring(0, str.LastIndexOf(strchar));
}
/// <summary>
/// 删除最后结尾的长度
/// </summary>
/// <param name="str"></param>
/// <param name="Length"></param>
/// <returns></returns>
public static string DelLastLength(string str, int Length)
{
if (string.IsNullOrEmpty(str))
return "";
str = str.Substring(0, str.Length - Length);
return str;
}
#endregion
#region 强制转换类型
/// <summary>
/// 强制转换类型
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
{
return from object item in source select (TResult)Convert.ChangeType(item, typeof(TResult));
}
#endregion
#region List转DataTable
/// <summary>
/// List转DataTable
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ListToDataTable<T>(IEnumerable<T> list)
{
//创建属性的集合
List<PropertyInfo> pList = new List<PropertyInfo>();
//获得反射的入口
Type type = typeof(T);
DataTable dt = new DataTable();
//把所有的public属性加入到集合 并添加DataTable的列
Array.ForEach<PropertyInfo>(type.GetProperties(), p =>
{
pList.Add(p);
var theType = p.PropertyType;
//处理可空类型
if (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
dt.Columns.Add(p.Name, Nullable.GetUnderlyingType(theType));
}
else
{
dt.Columns.Add(p.Name, theType);
}
});
foreach (var item in list)
{
//创建一个DataRow实例
DataRow row = dt.NewRow();
//给row 赋值
pList.ForEach(p =>
{
var v = p.GetValue(item, null);
row[p.Name] = v == null ? DBNull.Value : v;
});
//加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
/// <summary>
/// List转DataTable
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static DataTable ListToDataTableNew(List<dynamic> list)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
DataTable dt = new DataTable();
if (list != null && list.Count > 0)
{
foreach (var item in list[0])
{
string text = item.ToString();
string key = text.Substring(1, text.IndexOf(',') - 1);
string value = text.Substring(text.IndexOf(',') + 1, text.Length - text.IndexOf(',') - 2);
dt.Columns.Add(key);
}
}
foreach (var vlist in list)
{
DataRow row = dt.NewRow();
foreach (var item in vlist)
{
string text = item.ToString();
string key = text.Substring(1, text.IndexOf(',') - 1);
string value = text.Substring(text.IndexOf(',') + 1, text.Length - text.IndexOf(',') - 2);
//创建一个DataRow实例
row[key] = value;
}
//加入到DataTable
dt.Rows.Add(row);
}
return dt;
}
#endregion