Elin Decompiled Documentation EA 23.102 Nightly
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Pages
ClassCache.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq.Expressions;
4using System.Reflection;
5
6public class ClassCache<T>
7{
8 public Dictionary<string, Func<T>> dict = new Dictionary<string, Func<T>>();
9
10 public T Create<T2>(Type type)
11 {
12 if (type == null)
13 {
14 return default(T);
15 }
16 Func<T> func = dict.TryGetValue(type.Name);
17 if (func != null)
18 {
19 return func();
20 }
21 ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
22 if (constructor == null)
23 {
24 return default(T);
25 }
26 func = Expression.Lambda<Func<T>>(Expression.New(constructor), Array.Empty<ParameterExpression>()).Compile();
27 dict.Add(type.Name, func);
28 return func();
29 }
30
31 public T Create<T2>(string id, string assembly)
32 {
33 Type type = Type.GetType(id + ", " + assembly);
34 if (type == null)
35 {
36 foreach (string assembly2 in ClassCache.assemblies)
37 {
38 type = Type.GetType(id + ", " + assembly2);
39 if (type != null)
40 {
41 break;
42 }
43 }
44 }
45 return Create<T2>(type);
46 }
47}
48public class ClassCache
49{
51
52 public static HashSet<string> assemblies = new HashSet<string>();
53
54 public static T Create<T>(string id, string assembly = "Assembly-CSharp")
55 {
56 return (T)caches.Create<T>(id, assembly);
57 }
58}
static ClassCache< object > caches
Definition: ClassCache.cs:50
static HashSet< string > assemblies
Definition: ClassCache.cs:52
static T Create< T >(string id, string assembly="Assembly-CSharp")
Definition: ClassCache.cs:54
T Create< T2 >(Type type)
Definition: ClassCache.cs:10
Dictionary< string, Func< T > > dict
Definition: ClassCache.cs:8