-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyAccessor.cs
More file actions
58 lines (46 loc) · 1.88 KB
/
Copy pathAssemblyAccessor.cs
File metadata and controls
58 lines (46 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using InvvardDev.Ifttt.Contracts;
using Microsoft.Extensions.DependencyModel;
namespace InvvardDev.Ifttt.Reflection;
[ExcludeFromCodeCoverage(Justification = "It'd be equivalent to testing Assembly reflection")]
internal class AssemblyAccessor : IAssemblyAccessor
{
private List<Assembly>? applicationAssemblies;
private readonly List<string> assemblyNamesToFilterOut =
[
"Microsoft.",
"mscorlib",
"netstandard",
"Newtonsoft.Json",
"System",
"System.",
"WindowsBase"
];
public IEnumerable<Assembly> GetApplicationAssemblies()
{
if (applicationAssemblies is { } list) return list;
if (DependencyContext.Default is not { } defaultDependency) return new List<Assembly>();
applicationAssemblies =
[
.. defaultDependency
.GetDefaultAssemblyNames()
.Where(assembly => !IsFrameworkAssembly(assembly.Name))
.Select(Assembly.Load)
];
if (Assembly.GetEntryAssembly() is not { } entryAssembly) return applicationAssemblies;
if (!IsFrameworkAssembly(entryAssembly.GetName().Name)
&& applicationAssemblies.Exists(assembly => assembly.GetName() == entryAssembly.GetName()))
{
applicationAssemblies.Add(entryAssembly);
}
return applicationAssemblies;
}
public TAttribute? GetAttribute<TAttribute>(Type classType)
where TAttribute : Attribute
=> classType.GetCustomAttribute<TAttribute>();
public void FilterOutAssemblies(params string[] assemblyNames)
=> assemblyNamesToFilterOut.AddRange(assemblyNames);
private bool IsFrameworkAssembly(string? assemblyName)
=> !string.IsNullOrWhiteSpace(assemblyName) && assemblyNamesToFilterOut.Exists(a => a.StartsWith(assemblyName));
}