From 2006a9ea769b62d6f38d57a54a086ba422f9a8c5 Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Mon, 21 Sep 2020 22:45:33 +1000 Subject: [PATCH] Faster non-generic Il2Cpp casting --- src/Helpers/ReflectionHelpers.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Helpers/ReflectionHelpers.cs b/src/Helpers/ReflectionHelpers.cs index 1b9d122..403ed0c 100644 --- a/src/Helpers/ReflectionHelpers.cs +++ b/src/Helpers/ReflectionHelpers.cs @@ -22,15 +22,19 @@ namespace Explorer public static ILType ComponentType => Il2CppType.Of(); public static ILType BehaviourType => Il2CppType.Of(); - private static readonly MethodInfo m_tryCastMethodInfo = typeof(Il2CppObjectBase).GetMethod("TryCast"); + private static readonly MethodInfo tryCastMethodInfo = typeof(Il2CppObjectBase).GetMethod("TryCast"); + private static readonly Dictionary cachedTryCastMethods = new Dictionary(); public static object Il2CppCast(object obj, Type castTo) { if (!typeof(Il2CppSystem.Object).IsAssignableFrom(castTo)) return obj; - return m_tryCastMethodInfo - .MakeGenericMethod(castTo) - .Invoke(obj, null); + if (!cachedTryCastMethods.ContainsKey(castTo)) + { + cachedTryCastMethods.Add(castTo, tryCastMethodInfo.MakeGenericMethod(castTo)); + } + + return cachedTryCastMethods[castTo].Invoke(obj, null); } public static bool IsEnumerable(Type t)