using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
namespace Serein.Library.Utils
{
///
/// 对象转换工具类
///
public static class ObjectConvertHelper
{
///
/// 父类转为子类
///
/// 父类对象
/// 子类类型
///
public static object ConvertParentToChild(object parent, Type childType)
{
var child = Activator.CreateInstance(childType);
var parentType = parent.GetType();
// 复制父类属性
foreach (var prop in parentType.GetProperties())
{
if (prop.CanWrite)
{
var value = prop.GetValue(parent);
childType.GetProperty(prop.Name)?.SetValue(child, value);
}
}
return child;
}
///
/// 集合类型转换为Array/List
///
///
///
///
///
public static object ConvertToEnumerableType(object obj, Type targetType)
{
// 获取目标类型的元素类型
Type targetElementType = targetType.IsArray
? targetType.GetElementType()
: targetType.GetGenericArguments().FirstOrDefault();
if (targetElementType == null)
throw new InvalidOperationException("无法获取目标类型的元素类型");
// 检查输入对象是否为集合类型
if (obj is IEnumerable collection)
{
// 判断目标类型是否是数组
if (targetType.IsArray)
{
var toArrayMethod = typeof(Enumerable).GetMethod("ToArray").MakeGenericMethod(targetElementType);
return toArrayMethod.Invoke(null, new object[] { collection });
}
// 判断目标类型是否是 List
else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(List<>))
{
var toListMethod = typeof(Enumerable).GetMethod("ToList").MakeGenericMethod(targetElementType);
return toListMethod.Invoke(null, new object[] { collection });
}
// 判断目标类型是否是 HashSet
else if (targetType.IsGenericType && targetType.GetGenericTypeDefinition() == typeof(HashSet<>))
{
var toHashSetMethod = typeof(Enumerable).GetMethod("ToHashSet").MakeGenericMethod(targetElementType);
return toHashSetMethod.Invoke(null, new object[] { collection });
}
// 其他类型可以扩展类似的处理
}
throw new InvalidOperationException("输入对象不是集合或目标类型不支持");
}
///
/// 对象转换为对应类型
///
///
///
///
public static TResult ToConvert(this object data)
{
var type = typeof(TResult);
if (data is null && type.IsValueType)
{
return default;
}
return (TResult)data.ToConvertValueType(type);
}
///
/// 对象转换
///
///
///
///
public static object ToConvertValueType(this object data, Type type)
{
if (type.IsValueType)
{
if (data == null)
{
return null;
}
else
{
return ObjectConvertHelper.ValueParse(type, data);
}
}
return data;
}
///
/// 文本
///
///
///
///
public static T ValueParse(object value) where T : struct, IComparable
{
if (value is T data)
{
return data;
}
string valueStr = value.ToString();
return valueStr.ToValueData();
}
///
/// 文本转换数值
///
///
///
///
public static object ValueParse(Type type, object value)
{
string valueStr = value.ToString();
return valueStr.ToValueData(type);
}
///
/// 文本转换值对象
///
///
///
///
///
public static T ToValueData(this string valueStr) where T : struct, IComparable
{
if (string.IsNullOrEmpty(valueStr))
{
throw new NullReferenceException();
//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.IsNullOrWhiteSpace(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
else if (type == typeof(DateTime))
{
if (valueStr.Equals("now"))
{
return DateTime.Now;
}
else if (valueStr.Equals("utcnow"))
{
return DateTime.UtcNow;
}
return DateTime.Parse(valueStr);
}
else
{
throw new ArgumentException("非预期值类型");
}
return result;
}
}
}