| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
- //
- // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
- //
- // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
- namespace Admin.NET.Core;
- /// <summary>
- /// 对象拓展
- /// </summary>
- [SuppressSniffer]
- public static partial class ObjectExtension
- {
- /// <summary>
- /// 判断类型是否实现某个泛型
- /// </summary>
- /// <param name="type">类型</param>
- /// <param name="generic">泛型类型</param>
- /// <returns>bool</returns>
- 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);
- }
- /// <summary>
- /// 将字典转化为QueryString格式
- /// </summary>
- /// <param name="dict"></param>
- /// <param name="urlEncode"></param>
- /// <returns></returns>
- public static string ToQueryString(this Dictionary<string, string> dict, bool urlEncode = true)
- {
- return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}"));
- }
- /// <summary>
- /// 将字符串URL编码
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string UrlEncode(this string str)
- {
- return string.IsNullOrEmpty(str) ? "" : System.Uri.EscapeDataString(str);
- }
- /// <summary>
- /// 对象序列化成Json字符串
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string ToJson(this object obj)
- {
- return JSON.GetJsonSerializer().Serialize(obj);
- }
- /// <summary>
- /// Json字符串反序列化成对象
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="json"></param>
- /// <returns></returns>
- public static T ToObject<T>(this string json)
- {
- return JSON.GetJsonSerializer().Deserialize<T>(json);
- }
- /// <summary>
- /// 将object转换为long,若失败则返回0
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static long ParseToLong(this object obj)
- {
- try
- {
- return long.Parse(obj.ToString());
- }
- catch
- {
- return 0L;
- }
- }
- /// <summary>
- /// 将object转换为long,若失败则返回指定值
- /// </summary>
- /// <param name="str"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static long ParseToLong(this string str, long defaultValue)
- {
- try
- {
- return long.Parse(str);
- }
- catch
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// 将object转换为double,若失败则返回0
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static double ParseToDouble(this object obj)
- {
- try
- {
- return double.Parse(obj.ToString());
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 将object转换为double,若失败则返回指定值
- /// </summary>
- /// <param name="str"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- public static double ParseToDouble(this object str, double defaultValue)
- {
- try
- {
- return double.Parse(str.ToString());
- }
- catch
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// 将string转换为DateTime,若失败则返回日期最小值
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 将string转换为DateTime,若失败则返回默认值
- /// </summary>
- /// <param name="str"></param>
- /// <param name="defaultValue"></param>
- /// <returns></returns>
- 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();
- }
- }
- /// <summary>
- /// 将 string 时间日期格式转换成字符串 如 {yyyy} => 2024
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static string ParseToDateTimeForRep(this string str)
- {
- if (string.IsNullOrWhiteSpace(str))
- str = $"{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}";
- var date = DateTime.Now;
- var reg = new Regex(@"(\{.+?})");
- var match = reg.Matches(str);
- match.ToList().ForEach(u =>
- {
- var temp = date.ToString(u.ToString().Substring(1, u.Length - 2));
- str = str.Replace(u.ToString(), temp);
- });
- return str;
- }
- /// <summary>
- /// 是否有值
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static bool IsNullOrEmpty(this object obj)
- {
- return obj == null || string.IsNullOrEmpty(obj.ToString());
- }
- /// <summary>
- /// 字符串掩码
- /// </summary>
- /// <param name="str">字符串</param>
- /// <param name="mask">掩码符</param>
- /// <returns></returns>
- 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}")
- };
- }
- /// <summary>
- /// 身份证号掩码
- /// </summary>
- /// <param name="idCard">身份证号</param>
- /// <param name="mask">掩码符</param>
- /// <returns></returns>
- public static string MaskIdCard(this string idCard, char mask = '*')
- {
- if (!idCard.TryValidate(ValidationTypes.IDCard).IsValid) return idCard;
- var masks = mask.ToString().PadLeft(8, mask);
- return Regex.Replace(idCard, @"^(.{6})(.*)(.{4})$", $"$1{masks}$3");
- }
- /// <summary>
- /// 邮箱掩码
- /// </summary>
- /// <param name="email">邮箱</param>
- /// <param name="mask">掩码符</param>
- /// <returns></returns>
- 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(@"^([^\.]+)\.?", $"$1{masks}$2");
- }
- /// <summary>
- /// 将字符串转为值类型,如果没有得到或者错误返回为空
- /// </summary>
- /// <typeparam name="T">指定值类型</typeparam>
- /// <param name="str">传入字符串</param>
- /// <returns>可空值</returns>
- public static Nullable<T> ParseTo<T>(this string str) where T : struct
- {
- try
- {
- if (!string.IsNullOrWhiteSpace(str))
- {
- MethodInfo method = typeof(T).GetMethod("Parse", new Type[] { typeof(string) });
- if (method != null)
- {
- T result = (T)method.Invoke(null, new string[] { str });
- return result;
- }
- }
- }
- catch
- {
- }
- return null;
- }
- /// <summary>
- /// 将字符串转为值类型,如果没有得到或者错误返回为空
- /// </summary>
- /// <typeparam name="T">指定值类型</typeparam>
- /// <param name="str">传入字符串</param>
- /// <param name="type">目标类型</param>
- /// <returns>可空值</returns>
- public static object ParseTo(this string str, Type type)
- {
- try
- {
- if (type.Name == "String")
- {
- return str;
- }
- if (!string.IsNullOrWhiteSpace(str))
- {
- var _type = type;
- if (type.Name.StartsWith("Nullable"))
- {
- _type = type.GetGenericArguments()[0];
- }
- MethodInfo method = _type.GetMethod("Parse", new Type[] { typeof(string) });
- if (method != null)
- {
- return method.Invoke(null, new string[] { str });
- }
- }
- }
- catch
- {
- }
- return null;
- }
- /// <summary>
- /// 将一个的对象属性值赋给另一个制定的对象属性, 只复制相同属性的
- /// </summary>
- /// <param name="src">原数据对象</param>
- /// <param name="target">目标数据对象</param>
- /// <param name="changeProperties">属性集,键为原属性,值为目标属性</param>
- /// <param name="unChangeProperties">属性集,目标不修改的属性</param>
- public static void CopyTo(object src, object target, Dictionary<string, string> changeProperties = null, string[] unChangeProperties = null)
- {
- if (src == null || target == null)
- throw new ArgumentException("src == null || target == null ");
- var SourceType = src.GetType();
- var TargetType = target.GetType();
- if (changeProperties == null || changeProperties.Count == 0)
- {
- var fields = TargetType.GetProperties();
- changeProperties = fields.Select(m => m.Name).ToDictionary(m => m);
- }
- if (unChangeProperties == null || unChangeProperties.Length == 0)
- {
- foreach (var item in changeProperties)
- {
- var srcProperty = SourceType.GetProperty(item.Key);
- if (srcProperty != null)
- {
- var sourceVal = srcProperty
- .GetValue(src, null);
- var tarProperty = TargetType.GetProperty(item.Value);
- if (tarProperty != null)
- {
- tarProperty.SetValue(target, sourceVal, null);
- }
- }
- }
- }
- else
- {
- foreach (var item in changeProperties)
- {
- if (!unChangeProperties.Any(m => m == item.Value))
- {
- var srcProperty = SourceType.GetProperty(item.Key);
- if (srcProperty != null)
- {
- var sourceVal = srcProperty
- .GetValue(src, null);
- var tarProperty = TargetType.GetProperty(item.Value);
- if (tarProperty != null)
- {
- tarProperty.SetValue(target, sourceVal, null);
- }
- }
- }
- }
- }
- }
- }
|