/*********************************************************************** * Project: baifenBinfa.Net * * Web: https://baifenBinfa.com * * ProjectName: 百分兵法管理系统 * * Author: * * Email: * * Versions: 1.0 * * CreateTime: 2020-02-01 17:48:52 * NameSpace: CoreCms.Net.Framework.Utility.Extensions * FileName: ConvertExtensions * ClassDescription: ***********************************************************************/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Text; namespace CoreCms.Net.Utility.Extensions { /// /// 扩展数据转换 /// public static class ObjectExtensions { /// /// 数据转换为int类型 /// /// /// public static int ObjectToInt(this object thisValue) { int result = 0; if (thisValue == null) return 0; return thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out result) ? result : result; } /// /// 数据转换为int类型 /// /// /// /// public static int ObjectToInt(this object thisValue, int errorValue) { int result = 0; return thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out result) ? result : errorValue; } /// /// 数据转换为Double类型 /// /// /// public static double ObjectToDouble(this object thisValue) { double result = 0.0; return thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out result) ? result : 0.0; } /// /// 数据转换为Double类型 /// /// /// /// public static double ObjectToDouble(this object thisValue, double errorValue) { double result = 0.0; return thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out result) ? result : errorValue; } /// /// 数据转换为Float类型 /// /// /// public static float ObjectToFloat(this object thisValue) { float result = 0; return thisValue != null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out result) ? result : 0; } /// /// 数据转换为Float类型 /// /// /// /// public static float ObjectToFloat(this object thisValue, float errorValue) { float result = 0; return thisValue != null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out result) ? result : errorValue; } /// /// 数据转换为String类型 /// /// /// public static string ObjectToString(this object thisValue) { return thisValue != null ? thisValue.ToString().Trim() : ""; } /// /// 数据转换为String类型 /// /// /// /// public static string ObjectToString(this object thisValue, string errorValue) { return thisValue != null ? thisValue.ToString().Trim() : errorValue; } /// /// 数据转换为Decimal类型 /// /// /// public static Decimal ObjectToDecimal(this object thisValue) { Decimal result = new Decimal(); return thisValue != null && thisValue != DBNull.Value && Decimal.TryParse(thisValue.ToString(), out result) ? result : Decimal.Zero; } /// /// 数据转换为Decimal类型 /// /// /// /// public static Decimal ObjectToDecimal(this object thisValue, Decimal errorValue) { Decimal result = new Decimal(); return thisValue != null && thisValue != DBNull.Value && Decimal.TryParse(thisValue.ToString(), out result) ? result : errorValue; } /// /// 数据转换为DateTime类型 /// /// /// public static DateTime ObjectToDate(this object thisValue) { DateTime result = DateTime.MinValue; if (thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out result)) result = Convert.ToDateTime(thisValue); return result; } /// /// 数据转换为DateTime类型 /// /// /// /// public static DateTime ObjectToDate(this object thisValue, DateTime errorValue) { DateTime result = DateTime.MinValue; return thisValue != null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out result) ? result : errorValue; } /// /// 数据转换为bool类型 /// /// /// public static bool ObjectToBool(this object thisValue) { bool result = false; return thisValue != null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out result) ? result : result; } /// /// 获取枚举的标注的值 /// /// /// /// public static string GetDescription(this Enum value) { var field = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])field.GetCustomAttributes(typeof(DescriptionAttribute), false); return attributes.Length > 0 ? attributes[0].Description : value.ToString(); } } }