using MAX.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MAX.Core.Extensions;
///
/// 系统扩展 - 类型转换
///
public static partial class Extensions
{
///
/// IsNullOrEmpty
///
///
///
public static bool IsNullOrEmpty(this string input)
{
return string.IsNullOrEmpty(input);
}
///
/// 安全转换为字符串,去除两端空格,当值为null时返回""
///
/// 输入值
public static string SafeString(this object input)
{
return input?.ToString().Trim() ?? string.Empty;
}
///
/// 转换为bool
///
/// 数据
public static bool ToBool(this string obj, bool defvalue = false)
{
return Helpers.Convert.ToBool(obj, defvalue);
}
///
/// 转换为可空bool
///
/// 数据
public static bool? ToBoolOrNull(this string obj)
{
return Helpers.Convert.ToBoolOrNull(obj);
}
///
/// 转换为int
///
/// 数据
public static int ToInt(this string obj, int defvalue = 0)
{
return Helpers.Convert.ToInt(obj, defvalue);
}
///
/// 转换为可空int
///
/// 数据
public static int? ToIntOrNull(this string obj)
{
return Helpers.Convert.ToIntOrNull(obj);
}
///
/// 转换为long
///
/// 数据
public static long ToLong(this string obj, long defvalue = 0)
{
return Helpers.Convert.ToLong(obj, defvalue);
}
///
/// 转换为可空long
///
/// 数据
public static long? ToLongOrNull(this string obj)
{
return Helpers.Convert.ToLongOrNull(obj);
}
///
/// 转换为double
///
/// 数据
public static double ToDouble(this string obj, double defvalue = 0)
{
return Helpers.Convert.ToDouble(obj, defvalue: defvalue);
}
///
/// 转换为可空double
///
/// 数据
public static double? ToDoubleOrNull(this string obj)
{
return Helpers.Convert.ToDoubleOrNull(obj);
}
///
/// 转换为decimal
///
/// 数据
public static decimal ToDecimal(this string obj, decimal defvalue = 0)
{
return Helpers.Convert.ToDecimal(obj, defvalue: defvalue);
}
///
/// 转换为可空decimal
///
/// 数据
public static decimal? ToDecimalOrNull(this string obj)
{
return Helpers.Convert.ToDecimalOrNull(obj);
}
///
/// 转换为日期
///
/// 数据
public static DateTime ToDate(this string obj)
{
return Helpers.Convert.ToDate(obj);
}
///
/// 转换为可空日期
///
/// 数据
public static DateTime? ToDateOrNull(this string obj)
{
return Helpers.Convert.ToDateOrNull(obj);
}
///
/// 转换为Guid
///
/// 数据
public static Guid ToGuid(this string obj)
{
return Helpers.Convert.ToGuid(obj);
}
///
/// 转换为可空Guid
///
/// 数据
public static Guid? ToGuidOrNull(this string obj)
{
return Helpers.Convert.ToGuidOrNull(obj);
}
///
/// 转换为Guid集合
///
/// 数据,范例: "83B0233C-A24F-49FD-8083-1337209EBC9A,EAB523C6-2FE7-47BE-89D5-C6D440C3033A"
public static List ToGuidList(this string obj)
{
return Helpers.Convert.ToGuidList(obj);
}
///
/// 转换为Guid集合
///
/// 字符串集合
public static List ToGuidList(this IList obj)
{
if (obj == null)
{
return [];
}
return obj.Select(t => t.ToGuid()).ToList();
}
///
/// 转换枚举描述
///
///
///
public static string ToDescription(this Enum obj)
{
return EnumHelper.GetDescription(obj.GetType(), obj);
}
///
/// 枚举值 → 单个 EnumItem
///
///
///
///
public static EnumItem ToItem(this TEnum value) where TEnum : struct, Enum
{
return new EnumItem(value);
}
///
/// 是否单一标记
///
///
///
public static bool IsSingleFlag(Enum obj)
{
return EnumHelper.IsSingleFlag(obj);
}
///
/// 获取第一个标记
///
///
///
///
public static Enum GetFirstFlag(this Enum obj)
{
return EnumHelper.GetFirstFlag(obj);
}
}