2024-12-29 21:26:03 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using System;
|
2024-10-11 16:46:16 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Linq;
|
2024-12-18 00:05:42 +08:00
|
|
|
|
using System.Net.Http.Headers;
|
2024-10-11 16:46:16 +08:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Serein.Library.Utils
|
|
|
|
|
|
{
|
2024-10-11 19:31:34 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 类型转换工具类
|
|
|
|
|
|
/// </summary>
|
2024-10-11 16:46:16 +08:00
|
|
|
|
public static class ConvertHelper
|
|
|
|
|
|
{
|
2024-12-18 00:05:42 +08:00
|
|
|
|
|
2024-12-29 21:26:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对象转JSON文本
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="obj"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string ToJsonText(this object obj)
|
|
|
|
|
|
{
|
|
|
|
|
|
var jsonText = JsonConvert.SerializeObject(obj, Formatting.Indented);
|
|
|
|
|
|
return jsonText;
|
|
|
|
|
|
}
|
2024-12-18 00:05:42 +08:00
|
|
|
|
|
2024-12-29 21:26:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// JSON文本转对象
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">转换类型</typeparam>
|
|
|
|
|
|
/// <param name="json">JSON文本</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static T ToJsonObject<T>(this string json)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对象转换(好像没啥用)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TResult"></typeparam>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-10-11 16:46:16 +08:00
|
|
|
|
public static TResult ToConvert<TResult>(this object data)
|
|
|
|
|
|
{
|
|
|
|
|
|
var type = typeof(TResult);
|
|
|
|
|
|
if (data is null && type.IsValueType)
|
|
|
|
|
|
{
|
|
|
|
|
|
return default;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (TResult)data.ToConvert(type);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2024-12-29 21:26:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对象转换(好像没啥用)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-12-12 20:31:50 +08:00
|
|
|
|
public static object ToConvert(this object data, Type type)
|
2024-10-11 16:46:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (type.IsValueType)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (data == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Activator.CreateInstance(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertHelper.ValueParse(type, data);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return data;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-29 21:26:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 文本
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-10-11 16:46:16 +08:00
|
|
|
|
public static T ValueParse<T>(object value) where T : struct, IComparable<T>
|
|
|
|
|
|
{
|
|
|
|
|
|
string valueStr = value.ToString();
|
|
|
|
|
|
return valueStr.ToValueData<T>() ;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-29 21:26:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 文本转换数值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="type"></param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-10-11 16:46:16 +08:00
|
|
|
|
public static object ValueParse(Type type, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
string valueStr = value.ToString();
|
|
|
|
|
|
return valueStr.ToValueData(type);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-29 21:26:03 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 文本转换值对象
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="valueStr"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException"></exception>
|
2024-10-11 16:46:16 +08:00
|
|
|
|
public static T ToValueData<T>(this string valueStr) where T : struct, IComparable<T>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(valueStr))
|
|
|
|
|
|
{
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
var type = typeof(T);
|
|
|
|
|
|
object result;
|
|
|
|
|
|
if (type.IsEnum)
|
|
|
|
|
|
{
|
|
|
|
|
|
result = Enum.Parse(type, valueStr);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(bool))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = bool.Parse(valueStr);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(float))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(decimal))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(double))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(sbyte))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(byte))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(short))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(ushort))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(int))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(uint))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(long))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(ulong))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
#if NET6_0 || NET7_0 || NET8_0
|
|
|
|
|
|
else if (type == typeof(nint))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(nuint))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("非预期值类型");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (T)result;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static object ToValueData(this string valueStr, Type type)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(valueStr))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Activator.CreateInstance(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
object result;
|
|
|
|
|
|
if (type.IsEnum)
|
|
|
|
|
|
{
|
|
|
|
|
|
result = Enum.Parse(type, valueStr);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(bool))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = bool.Parse(valueStr);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(float))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = float.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(decimal))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = decimal.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(double))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = double.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(sbyte))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = sbyte.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(byte))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = byte.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(short))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = short.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(ushort))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = ushort.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(int))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = int.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(uint))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = uint.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(long))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = long.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(ulong))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = ulong.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
#if NET6_0 || NET7_0 || NET8_0
|
|
|
|
|
|
else if (type == typeof(nint))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = nint.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (type == typeof(nuint))
|
|
|
|
|
|
{
|
|
|
|
|
|
result = nuint.Parse(valueStr, CultureInfo.InvariantCulture);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|
2024-12-09 22:57:06 +08:00
|
|
|
|
else if(type == typeof(DateTime))
|
|
|
|
|
|
{
|
2024-12-18 00:05:42 +08:00
|
|
|
|
if (valueStr.Equals("now"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return DateTime.Now;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (valueStr.Equals("utcnow"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return DateTime.UtcNow;
|
|
|
|
|
|
}
|
2024-12-09 22:57:06 +08:00
|
|
|
|
return DateTime.Parse(valueStr);
|
|
|
|
|
|
}
|
2024-10-11 16:46:16 +08:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ArgumentException("非预期值类型");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|