Files
Workspace/Assets/Modules/ShrinkApp.Core/Runtime/ShrinkAppGeneratedRegistry.cs
T

54 lines
1.6 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
namespace ShrinkApp
{
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class ShrinkAppInstallerRegistryAttribute : Attribute
{
public ShrinkAppInstallerRegistryAttribute(params Type[] installerTypes)
{
InstallerTypes = installerTypes ?? Array.Empty<Type>();
}
public Type[] InstallerTypes { get; }
}
internal static class ShrinkAppGeneratedRegistry
{
public static IReadOnlyList<Type> GetInstallerTypes()
{
var types = new List<Type>();
var seen = new HashSet<Type>();
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
object[] attributes;
try
{
attributes = assembly.GetCustomAttributes(typeof(ShrinkAppInstallerRegistryAttribute), false);
}
catch
{
continue;
}
foreach (var attribute in attributes.OfType<ShrinkAppInstallerRegistryAttribute>())
{
foreach (var installerType in attribute.InstallerTypes ?? Array.Empty<Type>())
{
if (installerType == null || !seen.Add(installerType))
continue;
types.Add(installerType);
}
}
}
return types;
}
}
}