using System; using System.Linq; namespace EntrustSettle.Common.Helper { public static class GenericTypeExtensions { /// /// 判断类型是否实现某个泛型 /// /// 类型 /// 泛型类型 /// bool public static bool HasImplementedRawGeneric(this Type type, Type generic) { // 检查接口类型 var isTheRawGenericType = type.GetInterfaces().Any(IsTheRawGenericType); if (isTheRawGenericType) return true; // 检查类型 while (type != null && type != typeof(object)) { isTheRawGenericType = IsTheRawGenericType(type); if (isTheRawGenericType) return true; type = type.BaseType; } return false; // 判断逻辑 bool IsTheRawGenericType(Type t) => generic == (t.IsGenericType ? t.GetGenericTypeDefinition() : t); } public static string GetGenericTypeName(this Type type) { var typeName = string.Empty; if (type.IsGenericType) { var genericTypes = string.Join(",", type.GetGenericArguments().Select(t => t.Name).ToArray()); typeName = $"{type.Name.Remove(type.Name.IndexOf('`'))}<{genericTypes}>"; } else { typeName = type.Name; } return typeName; } public static string GetGenericTypeName(this object @object) { return @object.GetType().GetGenericTypeName(); } } }