You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.7 KiB
128 lines
3.7 KiB
using System.Collections.Concurrent; |
|
using System.Globalization; |
|
using Newtonsoft.Json; |
|
using Newtonsoft.Json.Linq; |
|
|
|
namespace BrilliantSightClient.Model.Extension; |
|
|
|
public static class CommonExtension |
|
{ |
|
/// <summary> |
|
/// 安全的ToString |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
/// <returns></returns> |
|
public static string ToSafeString(this object obj) |
|
{ |
|
if (null == obj) |
|
{ |
|
return ""; |
|
} |
|
else |
|
{ |
|
return obj.ToString(); |
|
} |
|
} |
|
// 缓存类型是否重写ToString的检查结果(提升性能) |
|
private static readonly ConcurrentDictionary<Type, bool> _toStringOverriddenCache = new(); |
|
|
|
/// <summary> |
|
/// 增强版安全ToString(自动处理未重写ToString的情况) |
|
/// </summary> |
|
public static string ToSafeAbundantString(this object obj) |
|
{ |
|
if (obj == null) return ""; |
|
|
|
var type = obj.GetType(); |
|
|
|
// 判断是否重写了ToString方法 |
|
var hasCustomToString = _toStringOverriddenCache.GetOrAdd(type, t => |
|
{ |
|
var toStringMethod = t.GetMethod("ToString", Type.EmptyTypes); |
|
return toStringMethod?.DeclaringType != typeof(object); |
|
}); |
|
|
|
try |
|
{ |
|
string result = hasCustomToString |
|
? obj.ToString() // 使用自定义的ToString |
|
: JsonConvert.SerializeObject(obj); |
|
return result; |
|
} |
|
catch (Exception e) |
|
{ |
|
return obj.ToString(); |
|
} |
|
|
|
|
|
} |
|
|
|
|
|
/// <summary> |
|
/// 简单的判断字符串是否为Null或空白 |
|
/// </summary> |
|
/// <param name="str"></param> |
|
/// <returns></returns> |
|
public static bool IsNullOrEmpty(this string str) |
|
{ |
|
return String.IsNullOrEmpty(str); |
|
} |
|
|
|
/// <summary> |
|
/// 生成对象签名(Null对象为时间戳) |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
/// <returns></returns> |
|
public static string GenerateSign(this object obj) |
|
{ |
|
if (null == obj) |
|
{ |
|
return Helper.Common.GenerateMd5Hash(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")); |
|
} |
|
else |
|
{ |
|
var str = $"{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")}-{obj.GetHashCode()}-{Helper.Common.AesKey}"; |
|
return Helper.Common.GenerateMd5Hash(str); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 字符串按类型转为JToken |
|
/// </summary> |
|
/// <param name="value"></param> |
|
/// <returns></returns> |
|
public static JToken ConvertJTokenValue(this string value) |
|
{ |
|
// 处理布尔类型(不区分大小写) |
|
if (string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)) |
|
return new JValue(true); |
|
if (string.Equals(value, "false", StringComparison.OrdinalIgnoreCase)) |
|
return new JValue(false); |
|
|
|
// 处理数值类型 |
|
if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out double numericValue)) |
|
{ |
|
// 判断是否为整数 |
|
if (numericValue == Math.Truncate(numericValue)) |
|
{ |
|
// 在int范围内转换为整型 |
|
if (numericValue >= int.MinValue && numericValue <= int.MaxValue) |
|
return new JValue((int)numericValue); |
|
|
|
// 超出int范围使用long类型 |
|
return new JValue((long)numericValue); |
|
} |
|
|
|
// 保留小数部分 |
|
return new JValue(numericValue); |
|
} |
|
|
|
// 默认处理为字符串 |
|
return new JValue(value); |
|
} |
|
|
|
// public static string ConvertNumberToString(this ob value) |
|
// { |
|
// return ""; |
|
// } |
|
} |