using System;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Text.RegularExpressions;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace MadRunFabric.Common
{
///
/// 类型帮助类
///
public static class ConvertHelper
{
#region 字符串转换
///
/// 获取非空字符串
///
///
///
///
public static string GetString(object obj, string str = "")
{
return (obj == null) ? str : obj.ToString();
}
///
/// 如果不是有效字符串就变成null
///
///
///
public static string EmptyAsNull(string str)
{
if (!ValidateHelper.IsPlumpStringAfterTrim(str))
{
return null;
}
return str;
}
#endregion
#region base64
///
/// 获取base64字符串
///
public static string GetBase64String(string str, Encoding encoding = null)
{
if (encoding == null) { encoding = Encoding.Default; }
return GetBase64String(encoding.GetBytes(str));
}
///
/// 获取base64字符串
///
public static string GetBase64String(byte[] bs)
{
return Convert.ToBase64String(bs);
}
///
/// base64字符串转为普通字符串
///
public static string GetStringFromBase64String(string str, Encoding encoding = null)
{
var bs = GetBytesFromBase64String(str);
return encoding.GetString(bs);
}
///
/// base64字符串转为byte[]
///
///
///
public static byte[] GetBytesFromBase64String(string str)
{
return Convert.FromBase64String(str);
}
#endregion
#region 数字转换
///
/// 转换为int类型
///
///
///
///
public static int GetInt(object obj, int? deft = default(int))
{
if (int.TryParse(GetString(obj), out int res))
{
return res;
}
return deft ?? throw new ArgumentException($"{obj}无法转为{res.GetType()}");
}
///
/// 转换为int64类型
///
///
///
///
public static Int64 GetInt64(object obj, Int64? deft = default(Int64))
{
if (Int64.TryParse(GetString(obj), out var res))
{
return res;
}
return deft ?? throw new ArgumentException($"{obj}无法转为{res.GetType()}");
}
///
/// 获取float类型
///
///
///
///
public static float GetFloat(object obj, float? deft = default(float))
{
if (float.TryParse(GetString(obj), out var res))
{
return res;
}
return deft ?? throw new ArgumentException($"{obj}无法转为{res.GetType()}");
}
///
/// 获取long类型
///
///
///
///
public static long GetLong(object obj, long? deft = default(long))
{
if (long.TryParse(GetString(obj), out var res))
{
return res;
}
return deft ?? throw new ArgumentException($"{obj}无法转为{res.GetType()}");
}
///
/// 获取double类型
///
///
///
///
public static double GetDouble(object obj, double? deft = default(double))
{
if (double.TryParse(GetString(obj), out var res))
{
return res;
}
return deft ?? throw new ArgumentException($"{obj}无法转为{res.GetType()}");
}
///
/// 获取decimal类型,常常用于货币
///
///
///
///
public static decimal GetDecimal(object obj, decimal? deft = default(decimal))
{
if (decimal.TryParse(GetString(obj), out var res))
{
return res;
}
return deft ?? throw new ArgumentException($"{obj}无法转为{res.GetType()}");
}
#endregion
#region 日期转换
///
/// 转换为日期格式
///
///
///
///
public static DateTime GetDateTime(object obj, DateTime? deft)
{
if (DateTime.TryParse(GetString(obj), out var res))
{
return res;
}
return deft ?? throw new ArgumentException($"{obj}无法转为{res.GetType()}");
}
///
/// 转换为日期格式
///
///
///
public static DateTime GetDateTime(object obj)
{
return GetDateTime(obj, DateTime.Now);
}
#endregion
#region 其他转换
///
/// 把ilist转换为list
/// 不返回null
///
///
///
///
public static List NotNullList(IEnumerable list) => list?.ToList() ?? new List() { };
[Obsolete]
public static List NotNullEnumerable(IEnumerable list) => ConvertHelper.NotNullList(list);
#endregion
#region 通用转换
///
/// 通用数据转换
///
///
///
///
public static T ChangeType(object obj, T deft = default(T))
{
try
{
var o = Convert.ChangeType(obj, typeof(T));
if (o is T re)
{
return re;
}
//强转
return (T)o;
}
catch
{
return deft;
}
}
#endregion
///
/// 从集合中删除元素
///
///
///
///
public static void Remove(this IEnumerable collection, T value)
{
(collection as List).Remove(value);
}
}
}