// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
namespace Admin.NET.Core;
///
/// 对象拓展
///
[SuppressSniffer]
public static partial class ObjectExtension
{
///
/// 判断类型是否实现某个泛型
///
/// 类型
/// 泛型类型
/// bool
public static bool HasImplementedRawGeneric(this Type type, Type generic)
{
// 检查接口类型
var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType);
if (isTheRawGenericType) return true;
// 检查类型
while (type != null && type != typeof(object))
{
isTheRawGenericType = IsTheRawGenericType(type);
if (isTheRawGenericType) return true;
type = type.BaseType;
}
return false;
// 判断逻辑
bool IsTheRawGenericType(Type type) => generic == (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
}
///
/// 将字典转化为QueryString格式
///
///
///
///
public static string ToQueryString(this Dictionary dict, bool urlEncode = true)
{
return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
}
///
/// 将字符串URL编码
///
///
///
public static string UrlEncode(this string str)
{
return string.IsNullOrEmpty(str) ? "" : System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8);
}
///
/// 对象序列化成Json字符串
///
///
///
public static string ToJson(this object obj)
{
return JSON.GetJsonSerializer().Serialize(obj);
}
///
/// Json字符串反序列化成对象
///
///
///
///
public static T ToObject(this string json)
{
return JSON.GetJsonSerializer().Deserialize(json);
}
///
/// 将object转换为long,若失败则返回0
///
///
///
public static long ParseToLong(this object obj)
{
try
{
return long.Parse(obj.ToString());
}
catch
{
return 0L;
}
}
///
/// 将object转换为long,若失败则返回指定值
///
///
///
///
public static long ParseToLong(this string str, long defaultValue)
{
try
{
return long.Parse(str);
}
catch
{
return defaultValue;
}
}
///
/// 将object转换为double,若失败则返回0
///
///
///
public static double ParseToDouble(this object obj)
{
try
{
return double.Parse(obj.ToString());
}
catch
{
return 0;
}
}
///
/// 将object转换为double,若失败则返回指定值
///
///
///
///
public static double ParseToDouble(this object str, double defaultValue)
{
try
{
return double.Parse(str.ToString());
}
catch
{
return defaultValue;
}
}
///
/// 将string转换为DateTime,若失败则返回日期最小值
///
///
///
public static DateTime ParseToDateTime(this string str)
{
try
{
if (string.IsNullOrWhiteSpace(str))
{
return DateTime.MinValue;
}
if (str.Contains('-') || str.Contains('/'))
{
return DateTime.Parse(str);
}
else
{
int length = str.Length;
switch (length)
{
case 4:
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
case 6:
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
case 8:
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
case 10:
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
case 12:
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
case 14:
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
default:
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
}
}
}
catch
{
return DateTime.MinValue;
}
}
///
/// 将string转换为DateTime,若失败则返回默认值
///
///
///
///
public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
{
try
{
if (string.IsNullOrWhiteSpace(str))
{
return defaultValue.GetValueOrDefault();
}
if (str.Contains('-') || str.Contains('/'))
{
return DateTime.Parse(str);
}
else
{
int length = str.Length;
switch (length)
{
case 4:
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
case 6:
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
case 8:
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
case 10:
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
case 12:
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
case 14:
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
default:
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
}
}
}
catch
{
return defaultValue.GetValueOrDefault();
}
}
///
/// 是否有值
///
///
///
public static bool IsNullOrEmpty(this object obj)
{
return obj == null || string.IsNullOrEmpty(obj.ToString());
}
///
/// 字符串掩码
///
/// 字符串
/// 掩码符
///
public static string Mask(this string str, char mask = '*')
{
if (string.IsNullOrWhiteSpace(str?.Trim()))
return str;
str = str.Trim();
var masks = mask.ToString().PadLeft(4, mask);
return str.Length switch
{
>= 11 => Regex.Replace(str, "(.{3}).*(.{4})", $"$1{masks}$2"),
10 => Regex.Replace(str, "(.{3}).*(.{3})", $"$1{masks}$2"),
9 => Regex.Replace(str, "(.{2}).*(.{3})", $"$1{masks}$2"),
8 => Regex.Replace(str, "(.{2}).*(.{2})", $"$1{masks}$2"),
7 => Regex.Replace(str, "(.{1}).*(.{2})", $"$1{masks}$2"),
6 => Regex.Replace(str, "(.{1}).*(.{1})", $"$1{masks}$2"),
_ => Regex.Replace(str, "(.{1}).*", $"$1{masks}")
};
}
///
/// 身份证号掩码
///
/// 身份证号
/// 掩码符
///
public static string MaskIdCard(this string idCard, char mask = '*')
{
if (!idCard.TryValidate(ValidationTypes.IDCard).IsValid) return idCard;
return idCard.Replace("(?<=\\w{3})\\w(?=\\w{4})", $"{mask}");
}
///
/// 邮箱掩码
///
/// 邮箱
/// 掩码符
///
public static string MaskEmail(this string email, char mask = '*')
{
if (!email.TryValidate(ValidationTypes.EmailAddress).IsValid) return email;
var masks = mask.ToString().PadLeft(4, mask);
return email.Replace("(^\\w)[^@]*(@.*$)", $"$1{masks}$2");
}
///
/// 银行卡号掩码
///
/// 银行卡号
/// 掩码符
///
public static string MaskBankCard(this string bankCard, char mask = '*')
{
if (bankCard.Length < 10) return bankCard;
var masks = mask.ToString().PadLeft(4, mask);
return bankCard.Replace("(\\d{6})\\d{9}(\\d{4})", $"$1{masks}$2");
}
}