ObjectUtils.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.ComponentModel;
  2. using System.Reflection;
  3. using System;
  4. using System.Text.RegularExpressions;
  5. namespace JiaZhiQuan.Common.Utils
  6. {
  7. public static class ObjectUtils
  8. {
  9. /// <summary>
  10. /// 将一个对象转换为指定类型
  11. /// </summary>
  12. /// <param name="obj">待转换的对象</param>
  13. /// <param name="type">目标类型</param>
  14. /// <returns>转换后的对象</returns>
  15. public static object ConvertObjectToType(object obj, Type type)
  16. {
  17. if (type == null) return obj;
  18. if (obj == null) return type.IsValueType ? Activator.CreateInstance(type) : null;
  19. // 如果目标类型为可空类型,则取可空类型的基类型.
  20. // case: int? -> int, DateTime? -> DateTime
  21. Type underlyingType = Nullable.GetUnderlyingType(type);
  22. // 如果待转换对象的类型与目标类型兼容,则无需转换
  23. // case: string -> string, int -> int, int -> object, int? -> int?, int? -> object
  24. if (type.IsAssignableFrom(obj.GetType()))
  25. {
  26. return obj;
  27. }
  28. // 如果待转换的对象的基类型为枚举
  29. else if ((underlyingType ?? type).IsEnum)
  30. {
  31. // 如果目标类型为可空枚举,且待转换对象为空字符串,则返回null
  32. if (underlyingType != null && string.IsNullOrEmpty(obj.ToString()))
  33. {
  34. return null;
  35. }
  36. else
  37. {
  38. // 如果目标类型为可空枚举,且待转换对象不为空字符串,则先将其转换为基类型的枚举,再转换为可空枚举
  39. return Enum.Parse(underlyingType ?? type, obj.ToString());
  40. }
  41. }
  42. // 如果目标类型为可转换类型
  43. else if (typeof(IConvertible).IsAssignableFrom(underlyingType ?? type))
  44. {
  45. try
  46. {
  47. // 将待转换对象转换为目标类型
  48. return Convert.ChangeType(obj, underlyingType ?? type);
  49. }
  50. catch
  51. {
  52. return underlyingType == null ? Activator.CreateInstance(type) : null;
  53. }
  54. }
  55. else
  56. {
  57. // 获取类型转换器
  58. TypeConverter converter = TypeDescriptor.GetConverter(type);
  59. // 判断是否支持从待转换对象类型到目标类型的转换
  60. if (converter.CanConvertFrom(obj.GetType()))
  61. {
  62. // 转换类型并返回
  63. return converter.ConvertFrom(obj);
  64. }
  65. ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
  66. if (constructor != null)
  67. {
  68. object o = constructor.Invoke(null);
  69. PropertyInfo[] propertys = type.GetProperties();
  70. Type oldType = obj.GetType();
  71. foreach (PropertyInfo property in propertys)
  72. {
  73. PropertyInfo p = oldType.GetProperty(property.Name);
  74. if (property.CanWrite && p != null && p.CanRead)
  75. {
  76. property.SetValue(o, ConvertObjectToType(p.GetValue(obj, null), property.PropertyType), null);
  77. }
  78. }
  79. return o;
  80. }
  81. }
  82. return obj;
  83. }
  84. private const int baseValue = 34;
  85. private const string digits = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  86. /// <summary>
  87. /// long转换为34进制
  88. /// </summary>
  89. /// <param name="number"></param>
  90. /// <returns></returns>
  91. public static string ConvertToBase34(this long number)
  92. {
  93. if (number == 0)
  94. return "0";
  95. string result = string.Empty;
  96. while (number != 0)
  97. {
  98. int remainder = (int)(number % baseValue);
  99. result = digits[remainder] + result;
  100. number /= baseValue;
  101. }
  102. return result;
  103. }
  104. /// <summary>
  105. /// 手机号码验证
  106. /// </summary>
  107. /// <param name="phoneNumber"></param>
  108. /// <returns></returns>
  109. public static bool IsPhoneNumber(string phoneNumber) {
  110. if(string.IsNullOrEmpty(phoneNumber)) return false;
  111. // 正则表达式模式,用于验证手机号码
  112. string pattern = @"^1[3-9]\d{9}$";
  113. Regex regex = new Regex(pattern);
  114. return regex.IsMatch(phoneNumber);
  115. }
  116. /// <summary>
  117. /// 34进制字符串转long
  118. /// </summary>
  119. /// <param name="base34"></param>
  120. /// <returns></returns>
  121. public static long ConvertFromBase34(this string base34)
  122. {
  123. if (base34 == "0")
  124. return 0;
  125. long result = 0;
  126. int power = 0;
  127. for (int i = base34.Length - 1; i >= 0; i--)
  128. {
  129. int digitValue = digits.IndexOf(base34[i]);
  130. result += digitValue * (long)Math.Pow(baseValue, power);
  131. power++;
  132. }
  133. return result;
  134. }
  135. }
  136. }