using System; using System.Collections.Generic; using System.Reflection; namespace RocketUtils { public static class AssemblyCache { private static readonly Dictionary Assemblies = new Dictionary(StringComparer.OrdinalIgnoreCase); public static bool TryGetAssembly(string assemblyName, out Assembly assembly) { WarmupAssembliesIfRequired(); return Assemblies.TryGetValue(assemblyName, out assembly); } public static bool TryGetAssemblyWithAttribute(out Assembly assembly) where T : Attribute { WarmupAssembliesIfRequired(); foreach (var asm in Assemblies.Values) { if (asm.GetCustomAttributes(typeof(T), false).Length > 0) { assembly = asm; return true; } } assembly = null; return false; } public static IEnumerable GetAssembliesWithAttribute() where T : Attribute { WarmupAssembliesIfRequired(); foreach (var asm in Assemblies.Values) { if (asm.GetCustomAttributes(typeof(T), false).Length > 0) { yield return asm; } } } private static void WarmupAssembliesIfRequired() { if (Assemblies.Count > 0) return; var assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (var assembly in assemblies) { Assemblies[assembly.FullName] = assembly; } } } }