using MAX.Core.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MAX.Core.Helpers; [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field)] public class ConvertAttribute : Attribute { public Type ConverterType { get; set; } public ConvertAttribute(Type type) { ConverterType = type; } } public interface IConverter { object Converter(object input); } /// /// 类型转换 /// public static partial class Convert { /// /// 转换为32位整型 /// /// 输入值 public static int ToInt(object input, int defvalue = 0) { return ToIntOrNull(input) ?? defvalue; } /// /// 转换为32位可空整型 /// /// 输入值 public static int? ToIntOrNull(object input) { bool success = int.TryParse(input.SafeString(), out int result); if (success) { return result; } try { double? temp = ToDoubleOrNull(input, 0); if (temp == null) { return null; } return System.Convert.ToInt32(temp); } catch { return null; } } /// /// 转换为64位整型 /// /// 输入值 public static long ToLong(object input, long defvalue = 0) { return ToLongOrNull(input) ?? defvalue; } /// /// 转换为64位可空整型 /// /// 输入值 public static long? ToLongOrNull(object input) { bool success = long.TryParse(input.SafeString(), out long result); if (success) { return result; } try { decimal? temp = ToDecimalOrNull(input, 0); if (temp == null) { return null; } return System.Convert.ToInt64(temp); } catch { return null; } } /// /// 转换为32位浮点型,并按指定小数位舍入 /// /// 输入值 /// 小数位数 public static float ToFloat(object input, int? digits = null) { return ToFloatOrNull(input, digits) ?? 0; } /// /// 转换为32位可空浮点型,并按指定小数位舍入 /// /// 输入值 /// 小数位数 public static float? ToFloatOrNull(object input, int? digits = null) { bool success = float.TryParse(input.SafeString(), out float result); if (!success) { return null; } if (digits == null) { return result; } return (float)Math.Round(result, digits.Value); } /// /// 转换为64位浮点型,并按指定小数位舍入 /// /// 输入值 /// 小数位数 public static double ToDouble(object input, int? digits = null, double defvalue = 0) { return ToDoubleOrNull(input, digits) ?? defvalue; } /// /// 转换为64位可空浮点型,并按指定小数位舍入 /// /// 输入值 /// 小数位数 public static double? ToDoubleOrNull(object input, int? digits = null) { if (input is bool b) { return b ? 1 : 0; } bool success = double.TryParse(input.SafeString(), out double result); if (!success) { return null; } if (digits == null) { return result; } return Math.Round(result, digits.Value); } /// /// 转换为128位浮点型,并按指定小数位舍入 /// /// 输入值 /// 小数位数 public static decimal ToDecimal(object input, int? digits = null, decimal defvalue = 0) { return ToDecimalOrNull(input, digits) ?? defvalue; } /// /// 转换为128位可空浮点型,并按指定小数位舍入 /// /// 输入值 /// 小数位数 public static decimal? ToDecimalOrNull(object input, int? digits = null) { bool success = decimal.TryParse(input.SafeString(), out decimal result); if (!success) { return null; } if (digits == null) { return result; } return Math.Round(result, digits.Value); } /// /// 转换为布尔值 /// /// 输入值 public static bool ToBool(object input, bool defvalue = false) { return ToBoolOrNull(input) ?? defvalue; } /// /// 转换为可空布尔值 /// /// 输入值 public static bool? ToBoolOrNull(object input) { bool? value = GetBool(input); if (value != null) { return value.Value; } return bool.TryParse(input.SafeString(), out bool result) ? result : null; } /// /// 获取布尔值 /// private static bool? GetBool(object input) { return input.SafeString().ToLower() switch { "0" => false, "否" => false, "不" => false, "no" => false, "fail" => false, "1" => true, "是" => true, "ok" => true, "yes" => true, _ => null, }; } /// /// 转换为日期 /// /// 输入值 public static DateTime ToDate(object input) { return ToDateOrNull(input) ?? DateTime.MinValue; } /// /// 转换为可空日期 /// /// 输入值 public static DateTime? ToDateOrNull(object input) { return DateTime.TryParse(input.SafeString(), out DateTime result) ? result : null; } /// /// 转换为Guid /// /// 输入值 public static Guid ToGuid(object input) { return ToGuidOrNull(input) ?? Guid.Empty; } /// /// 转换为可空Guid /// /// 输入值 public static Guid? ToGuidOrNull(object input) { return Guid.TryParse(input.SafeString(), out Guid result) ? result : null; } /// /// 转换为Guid集合 /// /// 以逗号分隔的Guid集合字符串,范例:83B0233C-A24F-49FD-8083-1337209EBC9A,EAB523C6-2FE7-47BE-89D5-C6D440C3033A public static List ToGuidList(string input) { return ToList(input); } /// /// 泛型集合转换 /// /// 目标元素类型 /// 以逗号分隔的元素集合字符串,范例:83B0233C-A24F-49FD-8083-1337209EBC9A,EAB523C6-2FE7-47BE-89D5-C6D440C3033A public static List ToList(string input) { List result = []; if (string.IsNullOrWhiteSpace(input)) { return result; } string[] array = input.Split(','); result.AddRange(from each in array where !string.IsNullOrWhiteSpace(each) select To(each)); return result; } /// /// 通用泛型转换 /// /// 目标类型 /// 输入值 public static T To(object input) { Type type = Common.GetType(); return (T)To(type, input); } public static object To(Type type, object input) { object defValue = null; if (type != null) { defValue = type.IsValueType ? Activator.CreateInstance(type) : null; } if (input == null) { return defValue; } if (input is string && string.IsNullOrWhiteSpace(input.ToString())) { return defValue; } string typeName = type.Name.ToLower(); try { if (typeName == "string") { return input.ToString(); } if (typeName == "guid") { return new Guid(input.ToString()); } if (type.IsEnum) { return EnumHelper.Parse(type, input); } if (typeof(IConverter).IsAssignableFrom(type)) { object instance = Activator.CreateInstance(type); return ((IConverter)instance).Converter(input); } if (input is IConvertible) { return System.Convert.ChangeType(input, type); } return input; } catch { return defValue; } } /// /// 转换为字节数组 /// /// 输入值 public static byte[] ToBytes(string input) { return ToBytes(input, Encoding.UTF8); } /// /// 转换为字节数组 /// /// 输入值 /// 字符编码 public static byte[] ToBytes(string input, Encoding encoding) { return string.IsNullOrWhiteSpace(input) ? new byte[] { } : encoding.GetBytes(input); } }