DragonECS/src/Internal/ReflectionUtility.cs

67 lines
2.3 KiB
C#
Raw Normal View History

2025-03-14 16:53:25 +08:00
#if DISABLE_DEBUG
#undef DEBUG
#endif
using System;
2024-03-06 21:35:34 +08:00
using System.Reflection;
using System.Runtime.CompilerServices;
2025-05-18 10:52:24 +08:00
namespace DCFApixels.DragonECS.Core.Internal
2024-03-06 21:35:34 +08:00
{
internal static class ReflectionUtility
{
2026-04-13 23:21:07 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Type GetPureType(this Type type)
{
if (type.IsGenericType)
{
return type.GetGenericTypeDefinition();
}
return type;
}
2024-03-06 21:35:34 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2026-04-15 14:37:16 +08:00
public static bool TryGetAttributeInherited<T>(this Type self, out T attribute, out Type declaringAtrType) where T : Attribute
2026-04-15 11:13:15 +08:00
{
if (self == null || self == typeof(object))
{
attribute = null;
2026-04-15 14:37:16 +08:00
declaringAtrType = null;
2026-04-15 11:13:15 +08:00
return false;
}
attribute = self.GetCustomAttribute<T>();
if (attribute == null)
{
2026-04-15 14:37:16 +08:00
return self.BaseType.TryGetAttributeInherited<T>(out attribute, out declaringAtrType);
2026-04-15 11:13:15 +08:00
}
2026-04-15 14:37:16 +08:00
declaringAtrType = self;
2026-04-15 11:13:15 +08:00
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-30 19:29:19 +08:00
public static bool TryGetAttribute<T>(this MemberInfo self, out T attribute) where T : Attribute
2024-03-06 21:35:34 +08:00
{
attribute = self.GetCustomAttribute<T>();
return attribute != null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-30 19:29:19 +08:00
public static bool TryGetAttribute<T>(this MemberInfo self, bool inherit, out T attribute) where T : Attribute
2024-03-06 21:35:34 +08:00
{
2024-09-30 19:29:19 +08:00
attribute = self.GetCustomAttribute<T>(inherit);
2024-03-06 21:35:34 +08:00
return attribute != null;
}
2024-09-30 19:29:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttribute<T>(this MemberInfo self) where T : Attribute
{
return self.GetCustomAttribute<T>() != null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool HasAttribute<T>(this MemberInfo self, bool inherit) where T : Attribute
{
return self.GetCustomAttribute<T>(inherit) != null;
}
public static bool IsCanInstantiated(this Type type)
{
return !type.IsAbstract && !type.IsInterface;
}
2024-03-06 21:35:34 +08:00
}
}