using Ant.Core.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Ant.Core.Utils { public class ConvertHelper { /// /// Bool类型转换(转换失败返回false) /// /// /// public static bool ToBool(object input) { return ToNullableBool(input) ?? false; } /// /// 转换为可空布尔值 /// /// 输入值 public static bool? ToNullableBool(object input) { bool? value = GetBool(input); if (value != null) return value.Value; return bool.TryParse(input.SafeString(), out var result) ? (bool?)result : null; } /// /// 获取布尔值 /// private static bool? GetBool(object input) { switch (input.SafeString().ToLower()) { case "0": return false; case "否": return false; case "不": return false; case "no": return false; case "fail": return false; case "1": return true; case "是": return true; case "ok": return true; case "yes": return true; default: return null; } } /// /// Byte类型转换(转换失败返回0) /// /// /// public static byte ToByte(object val) { return ToByte(val, 0); } /// /// Byte类型转换 /// /// /// 默认值 /// public static byte ToByte(object val, byte defaultValue) { byte num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is byte) { return (byte)val; } if (!byte.TryParse(val.ToString(), out num)) { return defaultValue; } return num; } /// /// 可空Byte类型转换 /// /// /// public static byte? ToByteNullable(object val) { byte num = ToByte(val); if (num.Equals((byte)0)) { return null; } return new byte?(num); } /// /// DateTime类型转换(转换失败返回1900-01-01) /// /// /// public static DateTime ToDateTime(object val) { DateTime time; if ((val == null) || (val == DBNull.Value)) { return new DateTime(1900, 1, 1); } if (val is DateTime) { return (DateTime)val; } if (!DateTime.TryParse(val.ToString(), out time)) { return new DateTime(1900, 1, 1); } return time; } /// /// DateTime类型转换 /// /// /// 默认值 /// public static DateTime ToDateTime(object val, DateTime defaultValue) { DateTime time; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is DateTime) { return (DateTime)val; } if (!DateTime.TryParse(val.ToString(), out time)) { return defaultValue; } return time; } /// /// 可空DateTime类型转换 /// /// /// public static DateTime? ToDateTimeNullable(object val) { DateTime time = ToDateTime(val); if (time.Equals(new DateTime(0x76c, 1, 1))) { return null; } return new DateTime?(time); } /// /// Decimal类型转换(默认精度2位小数,转化失败返回0) /// /// /// public static decimal ToDecimal(object val) { return ToDecimal(val, 0M, 2); } /// /// Decimal类型转换(转换失败返回0) /// /// /// 精度 /// public static decimal ToDecimal(object val, int decimals) { return ToDecimal(val, 0M, decimals); } /// /// Decimal类型转换 /// /// /// 默认值 /// 精度 /// public static decimal ToDecimal(object val, decimal defaultValue, int decimals) { decimal num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is decimal) { return Math.Round((decimal)val, decimals); } if (!decimal.TryParse(val.ToString(), out num)) { return defaultValue; } return Math.Round(num, decimals); } /// /// 可空Decimal转换(默认保留2位小数,转换失败返回0) /// /// /// public static decimal? ToDecimalNullable(object val) { decimal num = ToDecimal(val); if (num.Equals((decimal)0.0M)) { return null; } return new decimal?(num); } /// /// Double类型转换(默认精度2位小数,转换失败返回0) /// /// /// public static double ToDouble(object val) { return ToDouble(val, 0.0, 2); } /// /// Double类型转换(转换失败返回0) /// /// /// 精度 /// public static double ToDouble(object val, int digits) { return ToDouble(val, 0.0, digits); } /// /// Double类型转换 /// /// /// 默认值 /// 精度 /// public static double ToDouble(object val, double defaultValue, int digits) { double num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is double) { return Math.Round((double)val, digits); } if (!double.TryParse(val.ToString(), out num)) { return defaultValue; } return Math.Round(num, digits); } /// /// 可空Double类型转换(精度为2位小数,转换失败返回0) /// /// /// public static double? ToDoubleNullable(object val) { double num = ToDouble(val); if (num.Equals((double)0.0)) { return null; } return new double?(num); } /// /// 枚举类型转换(枚举值转换成枚举) /// /// /// /// public static T ToEnum(int value) { return (T)System.Enum.ToObject(typeof(T), value); } /// /// 枚举类型转换(枚举名称转换成枚举) /// /// /// /// public static T ToEnum(string name) { return (T)System.Enum.Parse(typeof(T), name, true); } /// /// Float类型转换(转换失败返回0) /// /// /// public static float ToFloat(object val) { return ToFloat(val, 0f); } /// /// Float类型转换 /// /// /// 默认值 /// public static float ToFloat(object val, float defaultValue) { float num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is float) { return (float)val; } if (!float.TryParse(val.ToString(), out num)) { return defaultValue; } return num; } /// /// 可空Float类型转换 /// /// /// public static float? ToFloatNullable(object val) { float num = ToFloat(val); if (num.Equals((float)0f)) { return null; } return new float?(num); } /// /// int类型转换(转换失败返回0) /// /// /// public static int ToInt(object val) { return ToInt(val, 0); } /// /// int类型转换 /// /// /// 默认值 /// public static int ToInt(object val, int defaultValue) { int num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is int) { return (int)val; } if (val.GetType().IsEnum) { return (int)val; } else { if (!int.TryParse(val.ToString(), out num)) { return defaultValue; } } return num; } /// /// 可空int类型转换 /// /// /// public static int? ToIntNullable(object val) { int num = ToInt(val); if (num.Equals(0)) { return null; } return new int?(num); } /// /// long类型转换(转换失败返回0) /// /// /// public static long ToLong(object val) { return ToLong(val, 0L); } /// /// long类型转换 /// /// /// 默认值 /// public static long ToLong(object val, long defaultValue) { long num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is long) { return (long)val; } if (!long.TryParse(val.ToString(), out num)) { return defaultValue; } return num; } /// /// 可空long类型转换 /// /// /// public static long? ToLongNullable(object val) { long num = ToLong(val); if (num.Equals((long)0L)) { return null; } return new long?(num); } /// /// byte类型转换(转换失败返回0) /// /// /// public static sbyte ToSbyte(object val) { return ToSbyte(val, 0); } /// /// byte类型转换 /// /// /// 默认值 /// public static sbyte ToSbyte(object val, sbyte defaultValue) { sbyte num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is sbyte) { return (sbyte)val; } if (!sbyte.TryParse(val.ToString(), out num)) { return defaultValue; } return num; } /// /// 可空byte类型转换 /// /// /// public static sbyte? ToSbyteNullable(object val) { sbyte num = ToSbyte(val); if (num.Equals((sbyte)0)) { return null; } return new sbyte?(num); } /// /// short类型转换(转换失败返回0) /// /// /// public static short ToShort(object val) { return ToShort(val, 0); } /// /// short类型转换 /// /// /// 默认值 /// public static short ToShort(object val, short defaultValue) { short num; if ((val == null) || (val == DBNull.Value)) { return defaultValue; } if (val is short) { return (short)val; } if (!short.TryParse(val.ToString(), out num)) { return defaultValue; } return num; } /// /// 可空short类型转换 /// /// /// public static short? ToShortNullable(object val) { short num = ToShort(val); if (num.Equals((short)0)) { return null; } return new short?(num); } /// /// string类型转换(转换失败返回string.Empty) /// /// /// public static string ToString(object val) { if ((val == null) || (val == DBNull.Value)) { return string.Empty; } if (val.GetType() == typeof(byte[])) { return Encoding.ASCII.GetString((byte[])val, 0, ((byte[])val).Length); } return val.ToString(); } /// /// string类型转换(替换字符) /// /// /// 替换字符 /// public static string ToString(object val, string replace) { string str = ToString(val); return (string.IsNullOrEmpty(str) ? replace : str); } /// /// string类型转换(转换失败返回null) /// /// /// public static string ToStringNullable(object val) { string str = ToString(val); return (string.IsNullOrEmpty(str) ? null : str); } /// /// 转换为Guid /// /// 输入值 public static Guid ToGuid(object input) { return ToGuidOrNull(input) ?? Guid.Empty; } /// /// 转换为可空Guid /// /// 输入值 public static Guid? ToGuidOrNull(object input) { return Guid.TryParse(input.SafeString(), out var result) ? (Guid?)result : null; } } }