#nullable enable using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace ShrinkCommand { [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] public sealed class ShrinkCommandStaticRegistryAttribute : Attribute { public ShrinkCommandStaticRegistryAttribute(params Type[] subscriberTypes) { SubscriberTypes = subscriberTypes ?? Array.Empty(); } public Type[] SubscriberTypes { get; } } internal static class ShrinkCommandGeneratedRegistry { public static IReadOnlyList GetStaticSubscriberTypes() { var types = new List(); var seen = new HashSet(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { object[] attributes; try { attributes = assembly.GetCustomAttributes(typeof(ShrinkCommandStaticRegistryAttribute), false); } catch { continue; } foreach (var attribute in attributes.OfType()) { foreach (var subscriberType in attribute.SubscriberTypes ?? Array.Empty()) { if (subscriberType == null || !seen.Add(subscriberType)) continue; types.Add(subscriberType); } } } return types; } public static void RegisterAll(ShrinkCommandService service) { if (service == null) throw new ArgumentNullException(nameof(service)); ShrinkCommandRegHelper.RegisterStaticCommandSubscriberTypes(service, GetStaticSubscriberTypes()); } } }