| | 1 | | using System.Diagnostics; |
| | 2 | | using System.Reflection; |
| | 3 | |
|
| | 4 | | namespace Pozitron.QuerySpecification; |
| | 5 | |
|
| | 6 | | internal static class TypeDiscovery |
| | 7 | | { |
| 3 | 8 | | private static readonly Lazy<bool> _isDebugBuild = new( |
| 3 | 9 | | () => |
| 3 | 10 | | { |
| 0 | 11 | | var debugAttribute = Assembly |
| 0 | 12 | | .GetEntryAssembly()? |
| 0 | 13 | | .GetCustomAttributes<DebuggableAttribute>() |
| 0 | 14 | | .FirstOrDefault(); |
| 3 | 15 | |
|
| 0 | 16 | | return debugAttribute is not null |
| 0 | 17 | | && (debugAttribute.DebuggingFlags & DebuggableAttribute.DebuggingModes.Default) == DebuggableAttribute.D |
| 3 | 18 | | }, |
| 3 | 19 | | LazyThreadSafetyMode.ExecutionAndPublication); |
| | 20 | |
|
| 3 | 21 | | private static readonly Lazy<Assembly[]> _loadedAssemblies = new( |
| 3 | 22 | | () => |
| 3 | 23 | | { |
| 3 | 24 | | try |
| 3 | 25 | | { |
| 3 | 26 | | return AppDomain.CurrentDomain |
| 3 | 27 | | .GetAssemblies() |
| 3 | 28 | | .Where(a => |
| 262 | 29 | | a.IsDynamic == false && |
| 262 | 30 | | a.FullName != null && |
| 262 | 31 | | !a.FullName.StartsWith("System", StringComparison.OrdinalIgnoreCase) && |
| 262 | 32 | | !a.FullName.StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase) && |
| 262 | 33 | | !a.FullName.StartsWith("netstandard", StringComparison.OrdinalIgnoreCase) && |
| 262 | 34 | | !a.FullName.StartsWith("Windows", StringComparison.OrdinalIgnoreCase) |
| 3 | 35 | | ) |
| 3 | 36 | | .ToArray(); |
| 3 | 37 | | } |
| 0 | 38 | | catch (Exception ex) |
| 3 | 39 | | { |
| 0 | 40 | | if (_isDebugBuild.Value) |
| 3 | 41 | | { |
| 0 | 42 | | throw new SpecAutoDiscoveryException(ex); |
| 3 | 43 | | } |
| 0 | 44 | | return []; |
| 3 | 45 | | } |
| 3 | 46 | | }, |
| 3 | 47 | | LazyThreadSafetyMode.ExecutionAndPublication); |
| | 48 | |
|
| 3 | 49 | | private static readonly Lazy<string> _autoDiscoveryValue = new( |
| 3 | 50 | | () => |
| 3 | 51 | | { |
| 3 | 52 | | var entryAssembly = Assembly.GetEntryAssembly(); |
| 3 | 53 | | var attr = entryAssembly?.GetCustomAttributes<SpecAutoDiscoveryAttribute>().FirstOrDefault(); |
| 3 | 54 | | if (attr is not null) return attr.Value; |
| 3 | 55 | |
|
| 83 | 56 | | foreach (var asm in _loadedAssemblies.Value) |
| 3 | 57 | | { |
| 39 | 58 | | attr = asm.GetCustomAttributes<SpecAutoDiscoveryAttribute>().FirstOrDefault(); |
| 40 | 59 | | if (attr is not null) return attr.Value; |
| 3 | 60 | | } |
| 3 | 61 | |
|
| 2 | 62 | | return string.Empty; |
| 3 | 63 | | }, |
| 3 | 64 | | LazyThreadSafetyMode.ExecutionAndPublication); |
| | 65 | |
|
| 3 | 66 | | private static readonly Lazy<bool> _isAutoDiscoveryEnabled = new( |
| 3 | 67 | | () => _autoDiscoveryValue.Value.Equals("enable", StringComparison.OrdinalIgnoreCase), |
| 3 | 68 | | LazyThreadSafetyMode.ExecutionAndPublication); |
| | 69 | |
|
| 3 | 70 | | private static readonly Lazy<List<IEvaluator>> _evaluators = new( |
| 2 | 71 | | () => GetInstancesOf<IEvaluator, DiscoveryAttribute>(), |
| 3 | 72 | | LazyThreadSafetyMode.ExecutionAndPublication); |
| | 73 | |
|
| 3 | 74 | | private static readonly Lazy<List<IMemoryEvaluator>> _memoryEvaluators = new( |
| 2 | 75 | | () => GetInstancesOf<IMemoryEvaluator, DiscoveryAttribute>(), |
| 3 | 76 | | LazyThreadSafetyMode.ExecutionAndPublication); |
| | 77 | |
|
| 3 | 78 | | private static readonly Lazy<List<IValidator>> _validators = new( |
| 2 | 79 | | () => GetInstancesOf<IValidator, DiscoveryAttribute>(), |
| 3 | 80 | | LazyThreadSafetyMode.ExecutionAndPublication); |
| | 81 | |
|
| | 82 | |
|
| 19 | 83 | | internal static bool IsAutoDiscoveryEnabled => _isAutoDiscoveryEnabled.Value; |
| 4 | 84 | | internal static List<IMemoryEvaluator> GetMemoryEvaluators() => _memoryEvaluators.Value.ToList(); |
| 4 | 85 | | internal static List<IEvaluator> GetEvaluators() => _evaluators.Value.ToList(); |
| 4 | 86 | | internal static List<IValidator> GetValidators() => _validators.Value.ToList(); |
| | 87 | |
|
| | 88 | | internal static List<TType> GetInstancesOf<TType, TAttribute>() |
| | 89 | | where TType : class |
| | 90 | | where TAttribute : DiscoveryAttribute |
| 6 | 91 | | => GetInstancesOf<TType, TAttribute>(_loadedAssemblies.Value); |
| | 92 | |
|
| | 93 | | internal static List<TType> GetInstancesOf<TType, TAttribute>(IEnumerable<Assembly> assemblies) |
| | 94 | | where TType : class |
| | 95 | | where TAttribute : DiscoveryAttribute |
| | 96 | | { |
| | 97 | | try |
| | 98 | | { |
| 14 | 99 | | var baseType = typeof(TType); |
| 14 | 100 | | var typeInstances = new List<(TType Instance, int Order, string TypeName)>(); |
| | 101 | |
|
| 14 | 102 | | var types = assemblies |
| 14 | 103 | | .SelectMany(a => |
| 14 | 104 | | { |
| 83 | 105 | | try { return a.GetTypes(); } catch { return Array.Empty<Type>(); } |
| 83 | 106 | | }) |
| 29068 | 107 | | .Where(t => t.IsClass && !t.IsAbstract && !t.ContainsGenericParameters && baseType.IsAssignableFrom(t)) |
| 14 | 108 | | .Distinct(); |
| | 109 | |
|
| 222 | 110 | | foreach (var type in types) |
| | 111 | | { |
| 97 | 112 | | var discoveryAttr = type.GetCustomAttribute<TAttribute>(); |
| 97 | 113 | | if (discoveryAttr is not null && !discoveryAttr.Enable) |
| | 114 | | continue; |
| | 115 | |
|
| 89 | 116 | | TType? instance = null; |
| | 117 | |
|
| 89 | 118 | | if (type.GetConstructor(Type.EmptyTypes) is not null) |
| | 119 | | { |
| 41 | 120 | | instance = (TType?)Activator.CreateInstance(type); |
| | 121 | | } |
| 48 | 122 | | else if (type.GetFields(BindingFlags.Public | BindingFlags.Static) |
| 32 | 123 | | .Where(f => type.IsAssignableFrom(f.FieldType)) |
| 48 | 124 | | .FirstOrDefault() is FieldInfo instanceField) |
| | 125 | | { |
| 32 | 126 | | instance = (TType?)instanceField.GetValue(null); |
| | 127 | | } |
| 16 | 128 | | else if (type.GetProperties(BindingFlags.Public | BindingFlags.Static) |
| 8 | 129 | | .Where(f => type.IsAssignableFrom(f.PropertyType)) |
| 16 | 130 | | .FirstOrDefault() is PropertyInfo instanceProp) |
| | 131 | | { |
| 8 | 132 | | instance = (TType?)instanceProp.GetValue(null); |
| | 133 | | } |
| | 134 | |
|
| 89 | 135 | | if (instance is null) continue; |
| | 136 | |
|
| 81 | 137 | | int order = discoveryAttr?.Order ?? int.MaxValue; |
| 81 | 138 | | typeInstances.Add((instance, order, type.Name)); |
| | 139 | | } |
| | 140 | |
|
| 14 | 141 | | return typeInstances |
| 81 | 142 | | .OrderBy(e => e.Order) |
| 81 | 143 | | .ThenBy(e => e.TypeName) |
| 81 | 144 | | .Select(e => e.Instance) |
| 14 | 145 | | .ToList(); |
| | 146 | | } |
| 0 | 147 | | catch (Exception ex) |
| | 148 | | { |
| 0 | 149 | | if (_isDebugBuild.Value) |
| | 150 | | { |
| 0 | 151 | | throw new SpecAutoDiscoveryException(ex); |
| | 152 | | } |
| 0 | 153 | | return []; |
| | 154 | | } |
| 14 | 155 | | } |
| | 156 | | } |