386 lines
8.3 KiB
C#
386 lines
8.3 KiB
C#
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);
|
|
}
|
|
/// <summary>
|
|
/// 类型转换
|
|
/// </summary>
|
|
public static partial class Convert
|
|
{
|
|
/// <summary>
|
|
/// 转换为32位整型
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static int ToInt(object input, int defvalue = 0)
|
|
{
|
|
return ToIntOrNull(input) ?? defvalue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为32位可空整型
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为64位整型
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static long ToLong(object input, long defvalue = 0)
|
|
{
|
|
return ToLongOrNull(input) ?? defvalue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为64位可空整型
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为32位浮点型,并按指定小数位舍入
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
/// <param name="digits">小数位数</param>
|
|
public static float ToFloat(object input, int? digits = null)
|
|
{
|
|
return ToFloatOrNull(input, digits) ?? 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为32位可空浮点型,并按指定小数位舍入
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
/// <param name="digits">小数位数</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为64位浮点型,并按指定小数位舍入
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
/// <param name="digits">小数位数</param>
|
|
public static double ToDouble(object input, int? digits = null, double defvalue = 0)
|
|
{
|
|
return ToDoubleOrNull(input, digits) ?? defvalue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为64位可空浮点型,并按指定小数位舍入
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
/// <param name="digits">小数位数</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为128位浮点型,并按指定小数位舍入
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
/// <param name="digits">小数位数</param>
|
|
public static decimal ToDecimal(object input, int? digits = null, decimal defvalue = 0)
|
|
{
|
|
return ToDecimalOrNull(input, digits) ?? defvalue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为128位可空浮点型,并按指定小数位舍入
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
/// <param name="digits">小数位数</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为布尔值
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static bool ToBool(object input, bool defvalue = false)
|
|
{
|
|
return ToBoolOrNull(input) ?? defvalue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为可空布尔值
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取布尔值
|
|
/// </summary>
|
|
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,
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为日期
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static DateTime ToDate(object input)
|
|
{
|
|
return ToDateOrNull(input) ?? DateTime.MinValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为可空日期
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static DateTime? ToDateOrNull(object input)
|
|
{
|
|
return DateTime.TryParse(input.SafeString(), out DateTime result) ? result : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为Guid
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static Guid ToGuid(object input)
|
|
{
|
|
return ToGuidOrNull(input) ?? Guid.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为可空Guid
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static Guid? ToGuidOrNull(object input)
|
|
{
|
|
return Guid.TryParse(input.SafeString(), out Guid result) ? result : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为Guid集合
|
|
/// </summary>
|
|
/// <param name="input">以逗号分隔的Guid集合字符串,范例:83B0233C-A24F-49FD-8083-1337209EBC9A,EAB523C6-2FE7-47BE-89D5-C6D440C3033A</param>
|
|
public static List<Guid> ToGuidList(string input)
|
|
{
|
|
return ToList<Guid>(input);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 泛型集合转换
|
|
/// </summary>
|
|
/// <typeparam name="T">目标元素类型</typeparam>
|
|
/// <param name="input">以逗号分隔的元素集合字符串,范例:83B0233C-A24F-49FD-8083-1337209EBC9A,EAB523C6-2FE7-47BE-89D5-C6D440C3033A</param>
|
|
public static List<T> ToList<T>(string input)
|
|
{
|
|
List<T> result = [];
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
{
|
|
return result;
|
|
}
|
|
|
|
string[] array = input.Split(',');
|
|
result.AddRange(from each in array where !string.IsNullOrWhiteSpace(each) select To<T>(each));
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通用泛型转换
|
|
/// </summary>
|
|
/// <typeparam name="T">目标类型</typeparam>
|
|
/// <param name="input">输入值</param>
|
|
public static T To<T>(object input)
|
|
{
|
|
Type type = Common.GetType<T>();
|
|
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;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 转换为字节数组
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
public static byte[] ToBytes(string input)
|
|
{
|
|
return ToBytes(input, Encoding.UTF8);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换为字节数组
|
|
/// </summary>
|
|
/// <param name="input">输入值</param>
|
|
/// <param name="encoding">字符编码</param>
|
|
public static byte[] ToBytes(string input, Encoding encoding)
|
|
{
|
|
return string.IsNullOrWhiteSpace(input) ? new byte[] { } : encoding.GetBytes(input);
|
|
}
|
|
}
|