Rewrite CultCache as attribute-first storage

This commit is contained in:
Meta Vens 2026-05-08 01:13:15 +01:00
commit da94389ee2
29 changed files with 1496 additions and 2488 deletions

View file

@ -5,55 +5,27 @@ using System.Reflection;
namespace GameCult.Caching
{
/// <summary>
/// Provides cached reflection helpers used by the cache infrastructure.
/// </summary>
public static class ReflectionExtensions
internal static class ReflectionExtensions
{
private static Dictionary<Type, Type[]> ParentTypes = new Dictionary<Type, Type[]>();
/// <summary>
/// Gets all base types for the supplied type, caching the result.
/// </summary>
/// <param name="type">The type whose parent types should be returned.</param>
/// <returns>An array of base types ordered from immediate parent upward.</returns>
public static Type[] GetParentTypes(this Type type)
public static IEnumerable<Type> GetAttributedDocumentTypes()
{
if (ParentTypes.ContainsKey(type))
return ParentTypes[type];
return ParentTypes[type] = type.GetParents().ToArray();
return AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => !assembly.IsDynamic)
.SelectMany(SafeGetTypes)
.Where(type => type is { IsAbstract: false, IsInterface: false })
.Where(type => type.GetCustomAttribute<CultDocumentAttribute>() != null);
}
private static IEnumerable<Type> GetParents(this Type type)
public static IEnumerable<Type> SafeGetTypes(Assembly assembly)
{
// is there any base type?
if (type == null)
try
{
yield break;
return assembly.GetTypes();
}
// return all inherited types
var currentBaseType = type.BaseType;
while (currentBaseType != null)
catch (ReflectionTypeLoadException ex)
{
yield return currentBaseType;
currentBaseType = currentBaseType.BaseType;
return ex.Types.Where(type => type != null)!;
}
}
private static Dictionary<Type, Type[]> ChildClasses = new Dictionary<Type, Type[]>();
/// <summary>
/// Gets all loaded types assignable to the supplied type, caching the result.
/// </summary>
/// <param name="type">The base type or interface to search from.</param>
/// <returns>An array of matching loaded types.</returns>
public static Type[] GetAllChildClasses(this Type type)
{
if (ChildClasses.ContainsKey(type))
return ChildClasses[type];
return ChildClasses[type] = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(ass => ass.GetTypes()).Where(type.IsAssignableFrom).ToArray();
}
}
}