// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。 // // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。 // // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任! namespace Admin.NET.Core; /// /// 对象转换扩展方法 /// public static class ParseExtensions { #region Bool /// /// 对象转布尔值 /// /// /// public static bool ParseToBool(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && bool.TryParse(thisValue.ToString(), out var reveal) && reveal; } #endregion Bool #region Short /// /// 对象转短整数 /// /// /// public static short ParseToShort(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && short.TryParse(thisValue.ToString(), out var reveal) ? reveal : (short)0; } /// /// 对象转短整数 /// /// /// /// public static short ParseToShort(this object? thisValue, short errorValue) { return thisValue is not null && thisValue != DBNull.Value && short.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion Short #region Long /// /// 对象转长整数 /// /// /// public static long ParseToLong(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out var reveal) ? reveal : 0L; } /// /// 对象转长整数 /// /// /// /// public static long ParseToLong(this object? thisValue, long errorValue) { return thisValue is not null && thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion Long #region Float /// /// 对象转浮点数 /// ±1.5 x 10 e−45 至 ±3.4 x 10 e38 大约 6-9 位数字 /// /// /// public static float ParseToFloat(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out var reveal) ? reveal : 0.0F; } /// /// 对象转浮点数 /// ±1.5 x 10 e−45 至 ±3.4 x 10 e38 大约 6-9 位数字 /// /// /// /// public static float ParseToFloat(this object? thisValue, float errorValue) { return thisValue is not null && thisValue != DBNull.Value && float.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion Float #region Double /// /// 对象转浮点数 /// ±5.0 × 10 e−324 到 ±1.7 × 10 e308 大约 15-17 位数字 /// /// /// public static double ParseToDouble(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out var reveal) ? reveal : 0.0D; } /// /// 对象转浮点数 /// ±5.0 × 10 e−324 到 ±1.7 × 10 e308 大约 15-17 位数字 /// /// /// /// public static double ParseToDouble(this object? thisValue, double errorValue) { return thisValue is not null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion Double #region Decimal /// /// 对象转浮点数 /// ±1.0 x 10 e-28 至 ±7.9228 x 10 e28 28-29 位 /// /// /// public static decimal ParseToDecimal(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out var reveal) ? reveal : 0M; } /// /// 对象转浮点数 /// ±1.0 x 10 e-28 至 ±7.9228 x 10 e28 28-29 位 /// /// /// /// public static decimal ParseToDecimal(this object? thisValue, decimal errorValue) { return thisValue is not null && thisValue != DBNull.Value && decimal.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion Decimal #region Int /// /// 对象转数字 /// /// /// public static int ParseToInt(this object? thisValue) { return thisValue is null ? 0 : thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out var reveal) ? reveal : 0; } /// /// 对象转数字 /// /// /// /// public static int ParseToInt(this object? thisValue, int errorValue) { return thisValue is not null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion Int #region Money /// /// 对象转金额 /// /// /// public static double ParseToMoney(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out var reveal) ? reveal : 0; } /// /// 对象转金额 /// /// /// /// public static double ParseToMoney(this object? thisValue, double errorValue) { return thisValue is not null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion Money #region String /// /// 对象转字符串 /// /// /// public static string ParseToString(this object? thisValue) { return thisValue is not null ? thisValue.ToString()!.Trim() : string.Empty; } /// /// 对象转字符串 /// /// /// /// public static string ParseToString(this object? thisValue, string errorValue) { return thisValue is not null ? thisValue.ToString()!.Trim() : errorValue; } /// /// 判断是否为空 /// /// /// public static bool IsEmptyOrNull(this object? thisValue) { return !thisValue.IsNotEmptyOrNull(); } /// /// 判断是否为空 /// /// /// public static bool IsNotEmptyOrNull(this object? thisValue) { return thisValue is not null && thisValue.ParseToString() != string.Empty && thisValue.ParseToString() != string.Empty && thisValue.ParseToString() != "undefined" && thisValue.ParseToString() != "null"; } /// /// 判断是否为空或零 /// /// /// public static bool IsNullOrZero(this object? thisValue) { return !thisValue.IsNotNullOrZero(); } /// /// 判断是否为空或零 /// /// /// public static bool IsNotNullOrZero(this object? thisValue) { return thisValue.IsNotEmptyOrNull() && thisValue.ParseToString() != "0"; } #endregion String #region DateTime /// /// 对象转日期 /// /// /// public static DateTime ParseToDateTime(this object? thisValue) { var reveal = DateTime.MinValue; if (thisValue is not null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out reveal)) { reveal = Convert.ToDateTime(thisValue); } return reveal; } /// /// 对象转日期 /// /// /// /// public static DateTime ParseToDateTime(this object? thisValue, DateTime errorValue) { return thisValue is not null && thisValue != DBNull.Value && DateTime.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion DateTime #region DateTimeOffset /// /// 对象转 DateTimeOffset /// /// /// public static DateTimeOffset ParseToDateTimeOffset(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && DateTimeOffset.TryParse(thisValue.ToString(), out var reveal) ? reveal : DateTimeOffset.MinValue; } /// /// 对象转 DateTimeOffset /// /// /// /// public static DateTimeOffset ParseToDateTimeOffset(this object? thisValue, DateTimeOffset errorValue) { return thisValue is not null && thisValue != DBNull.Value && DateTimeOffset.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion DateTimeOffset #region TimeSpan /// /// 对象转 DateTimeOffset /// /// /// public static TimeSpan ParseToTimeSpan(this object? thisValue) { return thisValue is not null && thisValue != DBNull.Value && TimeSpan.TryParse(thisValue.ToString(), out var reveal) ? reveal : TimeSpan.Zero; } /// /// 对象转 DateTimeOffset /// /// /// /// public static TimeSpan ParseToTimeSpan(this object? thisValue, TimeSpan errorValue) { return thisValue is not null && thisValue != DBNull.Value && TimeSpan.TryParse(thisValue.ToString(), out var reveal) ? reveal : errorValue; } #endregion TimeSpan #region Guid /// /// 将 string 转换为 Guid /// 若转换失败,则返回 Guid.Empty,不抛出异常。 /// /// /// public static Guid ParseToGuid(this object? thisValue) { try { return new Guid(thisValue.ParseToString()); } catch { return Guid.Empty; } } #endregion Guid #region Dictionary /// /// 对象转换成字典 /// /// /// public static IEnumerable> ParseToDictionary(this object? obj) { if (obj is not IEnumerable objDynamics) { yield break; } foreach (var objDynamic in objDynamics) { // 找到所有的没有此特性、或有此特性但忽略字段的属性 var item = (objDynamic as object).GetType().GetProperties() .ToDictionary(prop => prop.Name, prop => prop.GetValue(objDynamic, null)); yield return item; } } #endregion Dictionary }