CultLib/src/GameCult.Caching/ReflectionExtensions.cs
2026-05-08 01:13:15 +01:00

31 lines
923 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace GameCult.Caching
{
internal static class ReflectionExtensions
{
public static IEnumerable<Type> GetAttributedDocumentTypes()
{
return AppDomain.CurrentDomain.GetAssemblies()
.Where(assembly => !assembly.IsDynamic)
.SelectMany(SafeGetTypes)
.Where(type => type is { IsAbstract: false, IsInterface: false })
.Where(type => type.GetCustomAttribute<CultDocumentAttribute>() != null);
}
public static IEnumerable<Type> SafeGetTypes(Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types.Where(type => type != null)!;
}
}
}
}