blob: 9d3d3c0d31f6d1b2be2bfa7f5910550eaa86b78d (
plain) (
blame)
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
|
using System.Reflection;
namespace MatrixRoomUtils.Core.Extensions;
public class ClassCollector<T> where T : class {
static ClassCollector() {
if (!typeof(T).IsInterface)
throw new ArgumentException(
$"ClassCollector<T> must be used with an interface type. Passed type: {typeof(T).Name}");
}
public List<Type> ResolveFromAllAccessibleAssemblies() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(ResolveFromAssembly).ToList();
public List<Type> ResolveFromObjectReference(object obj) => ResolveFromTypeReference(obj.GetType());
public List<Type> ResolveFromTypeReference(Type t) => Assembly.GetAssembly(t)?.GetReferencedAssemblies().SelectMany(ResolveFromAssemblyName).ToList() ?? new List<Type>();
public List<Type> ResolveFromAssemblyName(AssemblyName assemblyName) => ResolveFromAssembly(Assembly.Load(assemblyName));
public List<Type> ResolveFromAssembly(Assembly assembly) => assembly.GetTypes()
.Where(x => x is { IsClass: true, IsAbstract: false } && x.GetInterfaces().Contains(typeof(T))).ToList();
// {
// List<Type> ret = new();
// foreach (var x in assembly.GetTypes().Where(x => x is { IsClass: true, IsAbstract: false } && x.GetInterfaces().Contains(typeof(T))).ToList()) {
// // Console.WriteLine($"[!!] Found class {x.FullName}");
// ret.Add(x);
// }
// return ret;
// }
}
|