-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathObjects.cs
56 lines (50 loc) · 2 KB
/
Objects.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using static System.Reflection.BindingFlags;
using static System.Linq.Enumerable;
namespace ExpressionTreeTestObjects {
public static class Objects {
static Objects() {
var safeTypes = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => (x.FullName ?? "").StartsWith("ExpressionTreeTestObjects"))
.SelectMany(x => {
var ret = Empty<Type>();
try {
ret = x.GetTypes();
} catch { }
return ret;
}).Where(x => x.HasAttribute<ObjectContainerAttribute>());
foreach (var t in safeTypes) {
LoadType(t);
}
}
private static readonly HashSet<(string category, string source, string name, object o)> objects = new();
private static readonly Dictionary<string, object> byName = new();
public static (string category, string source, string name, object o)[] Get() => objects.ToArray();
public static object ByName(string s) => byName[s];
public static void LoadType(Type t) {
LoadType(t, objects);
foreach (var (_, source, name, o) in objects) {
byName[$"{source}.{name}"] = o;
}
}
internal static void LoadType(Type t, ISet<(string category, string source, string name, object o)> objects) {
var source = t.Name;
t.GetFields(Static | NonPublic | Public)
.Select(fld => (
fld,
attr: fld.GetCustomAttribute<TestObjectAttribute>()
))
.WhereT((fld, attr) => attr is { })
.SelectT((fld, attr) => (
attr!.Category,
source,
fld.Name,
fld.GetValue(null)!
))
.AddRangeTo(objects);
}
}
}