Optimised ClassAttributeClass a bit more

This commit is contained in:
2026-01-24 22:26:59 +00:00
parent e905c1f7c5
commit 752b440e68

View File

@@ -22,52 +22,38 @@ namespace PashaBibko.Pacore
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
private static void ScanAllAssemblies() private static void ScanAllAssemblies()
{ {
using (CodeProfiler.Start("Assembly scan")) /* Fetches all the class types in all loaded assemblies */
Type[] classes = AppDomain.CurrentDomain.GetAssemblies() // Assembly[]
.SelectMany(assembly => assembly.GetTypes()) // IEnumerable<Type>
.Where(type => type.IsClass && !type.IsAbstract) // IEnumerable<Type>
.ToArray();
/* Allocates space for the cache */
AttributeCache = classes // Type[]
.Where(type => typeof(Attribute).IsAssignableFrom(type)) // IEnumerable<Type>
.ToDictionary
(
keySelector: t => t,
elementSelector: _ => new List<Type>()
); // Dictionary<Type, List<Type>>
/* Finds which attributes are attached to what classes */
HashSet<Type> seen = new();
foreach (Type current in classes)
{ {
/* Fetches all the class types in all loaded assemblies */ /* Tracks the seen attributes */
Type[] classes = AppDomain.CurrentDomain.GetAssemblies() // Assembly[] seen.Clear();
.SelectMany(assembly => foreach (object attr in current.GetCustomAttributes(inherit: true))
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException err)
{
return err.Types.Where(t => t != null);
}
}) // IEnumerable<Type>
.Where(type => type.IsClass && !type.IsAbstract) // IEnumerable<Type>
.ToArray();
/* Allocates space for the cache */
AttributeCache = classes // Type[]
.Where(type => typeof(Attribute).IsAssignableFrom(type)) // IEnumerable<Type>
.ToDictionary
(
keySelector: t => t,
elementSelector: _ => new List<Type>()
); // Dictionary<Type, List<Type>>
/* Finds which attributes are attached to what classes */
HashSet<Type> seen = new();
foreach (Type current in classes)
{ {
/* Tracks the seen attributes */ seen.Add(attr.GetType());
seen.Clear(); }
foreach (object attr in current.GetCustomAttributes(inherit: true))
{
seen.Add(attr.GetType());
}
/* Adds the class type to each attribute in the dictionary */ /* Adds the class type to each attribute in the dictionary */
foreach (Type type in seen) foreach (Type type in seen)
{ {
AttributeCache[type].Add(current); AttributeCache[type].Add(current);
}
} }
} }
} }
} }
} }