Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit 2006a9e

Browse files
committed
Faster non-generic Il2Cpp casting
1 parent c9bc450 commit 2006a9e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/Helpers/ReflectionHelpers.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ public class ReflectionHelpers
2222
public static ILType ComponentType => Il2CppType.Of<Component>();
2323
public static ILType BehaviourType => Il2CppType.Of<Behaviour>();
2424

25-
private static readonly MethodInfo m_tryCastMethodInfo = typeof(Il2CppObjectBase).GetMethod("TryCast");
25+
private static readonly MethodInfo tryCastMethodInfo = typeof(Il2CppObjectBase).GetMethod("TryCast");
26+
private static readonly Dictionary<Type, MethodInfo> cachedTryCastMethods = new Dictionary<Type, MethodInfo>();
2627

2728
public static object Il2CppCast(object obj, Type castTo)
2829
{
2930
if (!typeof(Il2CppSystem.Object).IsAssignableFrom(castTo)) return obj;
3031

31-
return m_tryCastMethodInfo
32-
.MakeGenericMethod(castTo)
33-
.Invoke(obj, null);
32+
if (!cachedTryCastMethods.ContainsKey(castTo))
33+
{
34+
cachedTryCastMethods.Add(castTo, tryCastMethodInfo.MakeGenericMethod(castTo));
35+
}
36+
37+
return cachedTryCastMethods[castTo].Invoke(obj, null);
3438
}
3539

3640
public static bool IsEnumerable(Type t)

0 commit comments

Comments
 (0)