| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.Injection; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides helper methods for filtering types during dependency injection scanning. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class TypeFiltering |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Determines if a type is a concrete type suitable for registration. |
| | | 12 | | /// This filters out abstract classes, interfaces, nested types, delegates, exceptions, attributes, etc. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="type">The type to check.</param> |
| | | 15 | | /// <returns>True if the type is a concrete type that can be instantiated.</returns> |
| | | 16 | | [RequiresUnreferencedCode("IsConcreteType calls IsRecord which uses reflection.")] |
| | | 17 | | public static bool IsConcreteType(Type type) => |
| | 672179 | 18 | | type.IsClass && |
| | 672179 | 19 | | !type.IsAbstract && |
| | 672179 | 20 | | !type.IsGenericTypeDefinition && |
| | 672179 | 21 | | !type.IsFunctionPointer && |
| | 672179 | 22 | | !type.IsValueType && |
| | 672179 | 23 | | !type.IsInterface && |
| | 672179 | 24 | | !type.IsCOMObject && |
| | 672179 | 25 | | !type.IsEnum && |
| | 672179 | 26 | | !type.IsNested && |
| | 672179 | 27 | | !type.IsAssignableTo(typeof(MulticastDelegate)) && |
| | 672179 | 28 | | !type.IsAssignableTo(typeof(Exception)) && |
| | 672179 | 29 | | !type.IsAssignableTo(typeof(Attribute)) && |
| | 672179 | 30 | | !IsRecord(type) && |
| | 672179 | 31 | | type.Name[0] != '<' && type.Name[^1] != '>' //t.GetCustomAttribute<CompilerGeneratedAttribute>() is null |
| | | 32 | | ; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Determines if a type is a record type. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="type">The type to check.</param> |
| | | 38 | | /// <returns>True if the type is a record.</returns> |
| | | 39 | | [RequiresUnreferencedCode("IsRecord uses Type.GetMethod which requires reflection.")] |
| | | 40 | | public static bool IsRecord(Type type) |
| | | 41 | | { |
| | 123956 | 42 | | return type.IsClass && |
| | 123956 | 43 | | type.GetMethod("<Clone>$") is not null; |
| | | 44 | | } |
| | | 45 | | } |