2025-04-21 10:17:40 +08:00
|
|
|
|
using System.Collections;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
namespace JiShe.CollectBus.Kafka.Internal;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 反射辅助类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class ReflectionHelper
|
2025-04-19 00:30:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-04-21 10:17:40 +08:00
|
|
|
|
/// 集合类型
|
|
|
|
|
|
/// Item1:参数类型
|
|
|
|
|
|
/// Item2:集合元素类型
|
2025-04-19 00:30:58 +08:00
|
|
|
|
/// </summary>
|
2025-04-21 10:17:40 +08:00
|
|
|
|
public static Tuple<Type, Type?> GetParameterTypeInfo(this MethodInfo method, int parameterIndex = 0)
|
2025-04-19 00:30:58 +08:00
|
|
|
|
{
|
2025-04-21 10:17:40 +08:00
|
|
|
|
// 参数校验
|
|
|
|
|
|
if (method == null) throw new ArgumentNullException(nameof(method));
|
|
|
|
|
|
var parameters = method.GetParameters();
|
|
|
|
|
|
if (parameterIndex < 0 || parameterIndex >= parameters.Length)
|
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(parameterIndex));
|
|
|
|
|
|
|
|
|
|
|
|
var param = parameters[parameterIndex];
|
|
|
|
|
|
var paramType = param.ParameterType;
|
|
|
|
|
|
Type? elementType = null;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
// 判断是否是集合类型(排除字符串)
|
|
|
|
|
|
if (paramType != typeof(string) && IsEnumerableType(paramType))
|
|
|
|
|
|
elementType = GetEnumerableElementType(paramType);
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
return Tuple.Create(paramType, elementType);
|
|
|
|
|
|
}
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 判断是否是集合类型(排除字符串)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static bool IsEnumerableType(this Type type)
|
|
|
|
|
|
{
|
|
|
|
|
|
return type.IsArray
|
|
|
|
|
|
|| (type.IsGenericType && type.GetInterfaces()
|
|
|
|
|
|
.Any(t => t.IsGenericType
|
|
|
|
|
|
&& t.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
|
|
|
|
|
|
|| type.GetInterfaces().Any(t => t == typeof(IEnumerable));
|
|
|
|
|
|
}
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取集合元素的类型
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static Type? GetEnumerableElementType(this Type type)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 处理数组类型
|
|
|
|
|
|
if (type.IsArray)
|
|
|
|
|
|
return type.GetElementType();
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
// 处理直接实现IEnumerable<T>的类型(如IEnumerable<int>本身)
|
|
|
|
|
|
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
|
|
|
|
|
|
return type.GetGenericArguments()[0];
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
// 处理通过接口实现IEnumerable<T>的泛型集合(如List<T>)
|
|
|
|
|
|
var genericEnumerable = type.GetInterfaces()
|
|
|
|
|
|
.FirstOrDefault(t => t.IsGenericType
|
|
|
|
|
|
&& t.GetGenericTypeDefinition() == typeof(IEnumerable<>));
|
|
|
|
|
|
if (genericEnumerable != null)
|
|
|
|
|
|
return genericEnumerable.GetGenericArguments()[0];
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
// 处理非泛型集合类型(如 ArrayList)
|
|
|
|
|
|
if (typeof(IEnumerable).IsAssignableFrom(type) && type == typeof(ArrayList))
|
|
|
|
|
|
return typeof(ArrayList);
|
|
|
|
|
|
// 返回null表示无法确定元素类型
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 判断是否使用强转换
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="targetType"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static bool IsConvertType(this Type targetType)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 处理可空类型
|
|
|
|
|
|
var underlyingType = Nullable.GetUnderlyingType(targetType) ?? targetType;
|
|
|
|
|
|
// 情况1:值类型或基元类型(如 int、DateTime)
|
|
|
|
|
|
if (underlyingType.IsValueType || underlyingType.IsPrimitive)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
// 情况2:字符串类型直接赋值
|
|
|
|
|
|
if (underlyingType == typeof(string))
|
|
|
|
|
|
return true;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
|
2025-04-21 10:17:40 +08:00
|
|
|
|
// 情况3:枚举类型处理
|
|
|
|
|
|
//else if (underlyingType.IsEnum)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (Enum.IsDefined(underlyingType, msg))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// convertedValue = Enum.Parse(underlyingType, msg.ToString());
|
|
|
|
|
|
// return true;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
//}
|
|
|
|
|
|
return false;
|
2025-04-19 00:30:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|