From 57164d886f988678d8428c1ee085d221a00be973 Mon Sep 17 00:00:00 2001 From: hechao Date: Tue, 14 Mar 2023 19:47:42 +0800 Subject: [PATCH] fix: https://github.com/Tencent/InjectFix/issues/384 --- Source/VSProj/Src/Core/Utils.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/Source/VSProj/Src/Core/Utils.cs b/Source/VSProj/Src/Core/Utils.cs index 746451b..8bc130b 100644 --- a/Source/VSProj/Src/Core/Utils.cs +++ b/Source/VSProj/Src/Core/Utils.cs @@ -50,7 +50,8 @@ public static bool IsAssignable(MethodInfo delegateMethod, MethodInfo method) } //适配器的缓存,如果不做缓存,每次都调用IsAssignable一个个的取匹配会非常慢 - static Dictionary delegateAdptCache = new Dictionary(); + static Dictionary> delegateAdptCache = + new Dictionary>(); /// /// 从一个wrapper对象里头,查找能够适配到特定delegate的方法 @@ -62,20 +63,28 @@ public static bool IsAssignable(MethodInfo delegateMethod, MethodInfo method) public static Delegate TryAdapterToDelegate(object obj, Type delegateType, string perfix) { MethodInfo method; - if (!delegateAdptCache.TryGetValue(delegateType, out method)) + if (!delegateAdptCache.TryGetValue(obj.GetType().Assembly.FullName, out var cache)) + { + cache = new Dictionary(); + delegateAdptCache.Add(obj.GetType().Assembly.FullName, cache); + } + + if (!cache.TryGetValue(delegateType, out method)) { MethodInfo delegateMethod = delegateType.GetMethod("Invoke"); var methods = obj.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance - | BindingFlags.DeclaredOnly); + | BindingFlags.DeclaredOnly); for (int i = 0; i < methods.Length; i++) { if (methods[i].Name.StartsWith(perfix) && IsAssignable(delegateMethod, methods[i])) { method = methods[i]; - delegateAdptCache[delegateType] = method; + cache[delegateType] = method; } } } + + if (method == null) { return null;