CultLib/src/GameCult.Caching/ReflectionExtensions.cs

31 lines
923 B
C#
Raw Normal View History

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