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

Commit e0fd682

Browse files
committed
Add MethodInfo helper
1 parent 7426bd1 commit e0fd682

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/Core/ReflectionUtility.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,51 @@ public static PropertyInfo GetPropertyInfo(Type type, string propertyName)
192192
return s_cachedPropInfos[type][propertyName];
193193
}
194194

195+
internal static Dictionary<Type, Dictionary<string, MethodInfo>> s_cachedMethodInfos = new Dictionary<Type, Dictionary<string, MethodInfo>>();
196+
197+
public static MethodInfo GetMethodInfo(Type type, string methodName, Type[] argumentTypes)
198+
{
199+
if (!s_cachedMethodInfos.ContainsKey(type))
200+
s_cachedMethodInfos.Add(type, new Dictionary<string, MethodInfo>());
201+
202+
var sig = methodName;
203+
204+
if (argumentTypes != null)
205+
{
206+
sig += "(";
207+
for (int i = 0; i < argumentTypes.Length; i++)
208+
{
209+
if (i > 0)
210+
sig += ",";
211+
sig += argumentTypes[i].FullName;
212+
}
213+
sig += ")";
214+
}
215+
216+
try
217+
{
218+
if (!s_cachedMethodInfos[type].ContainsKey(sig))
219+
{
220+
if (argumentTypes != null)
221+
s_cachedMethodInfos[type].Add(sig, type.GetMethod(methodName, AllFlags, null, argumentTypes, null));
222+
else
223+
s_cachedMethodInfos[type].Add(sig, type.GetMethod(methodName, AllFlags));
224+
}
225+
226+
return s_cachedMethodInfos[type][sig];
227+
}
228+
catch (AmbiguousMatchException)
229+
{
230+
ExplorerCore.LogWarning($"AmbiguousMatchException trying to get method '{sig}'");
231+
return null;
232+
}
233+
catch (Exception e)
234+
{
235+
ExplorerCore.LogWarning($"{e.GetType()} trying to get method '{sig}': {e.Message}\r\n{e.StackTrace}");
236+
return null;
237+
}
238+
}
239+
195240
/// <summary>
196241
/// Helper to display a simple "{ExceptionType}: {Message}" of the exception, and optionally use the inner-most exception.
197242
/// </summary>

0 commit comments

Comments
 (0)