diff --git a/src/Core/ReflectionUtility.cs b/src/Core/ReflectionUtility.cs index 863b1ee..b7e8a8f 100644 --- a/src/Core/ReflectionUtility.cs +++ b/src/Core/ReflectionUtility.cs @@ -192,6 +192,51 @@ namespace UnityExplorer return s_cachedPropInfos[type][propertyName]; } + internal static Dictionary> s_cachedMethodInfos = new Dictionary>(); + + public static MethodInfo GetMethodInfo(Type type, string methodName, Type[] argumentTypes) + { + if (!s_cachedMethodInfos.ContainsKey(type)) + s_cachedMethodInfos.Add(type, new Dictionary()); + + var sig = methodName; + + if (argumentTypes != null) + { + sig += "("; + for (int i = 0; i < argumentTypes.Length; i++) + { + if (i > 0) + sig += ","; + sig += argumentTypes[i].FullName; + } + sig += ")"; + } + + try + { + if (!s_cachedMethodInfos[type].ContainsKey(sig)) + { + if (argumentTypes != null) + s_cachedMethodInfos[type].Add(sig, type.GetMethod(methodName, AllFlags, null, argumentTypes, null)); + else + s_cachedMethodInfos[type].Add(sig, type.GetMethod(methodName, AllFlags)); + } + + return s_cachedMethodInfos[type][sig]; + } + catch (AmbiguousMatchException) + { + ExplorerCore.LogWarning($"AmbiguousMatchException trying to get method '{sig}'"); + return null; + } + catch (Exception e) + { + ExplorerCore.LogWarning($"{e.GetType()} trying to get method '{sig}': {e.Message}\r\n{e.StackTrace}"); + return null; + } + } + /// /// Helper to display a simple "{ExceptionType}: {Message}" of the exception, and optionally use the inner-most exception. ///