WKLib 0.2.3
A modding library for White Knuckle
Loading...
Searching...
No Matches
ReflectionUtility.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4
6
7// https://github.com/yukieiji/UniverseLib/blob/main/src/Reflection/ReflectionUtility.cs
8
9internal class ReflectionUtility
10{
12 private static readonly SortedDictionary<string, Type> AllTypes = new(StringComparer.OrdinalIgnoreCase);
13
14 internal static void Initialize()
15 {
16 SetupTypeCache();
17 }
18
19 internal static Type GetTypeByName(string fullName)
20 {
21 AllTypes.TryGetValue(fullName, out Type type);
22
23 if (type == null)
24 type = Type.GetType(fullName);
25
26 return type;
27 }
28
29 private static void SetupTypeCache()
30 {
31 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
32 {
33 CacheTypes(asm);
34 }
35 }
36
37 private static void CacheTypes(Assembly asm)
38 {
39 Type[] asmTypes = null;
40 try
41 {
42 asmTypes = asm.GetTypes();
43 }
44 catch
45 {
46 return;
47 }
48 if (asmTypes == null)
49 return;
50
51 foreach (Type type in asmTypes)
52 {
53 // Cache the type. Overwrite type if one exists with the full name
54 AllTypes[type.FullName] = type;
55 }
56 }
57}