Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #384 #391

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions Source/VSProj/Src/Core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static bool IsAssignable(MethodInfo delegateMethod, MethodInfo method)
}

//适配器的缓存,如果不做缓存,每次都调用IsAssignable一个个的取匹配会非常慢
static Dictionary<Type, MethodInfo> delegateAdptCache = new Dictionary<Type, MethodInfo>();
static Dictionary<string, Dictionary<Type, MethodInfo>> delegateAdptCache =
new Dictionary<string, Dictionary<Type, MethodInfo>>();

/// <summary>
/// 从一个wrapper对象里头,查找能够适配到特定delegate的方法
Expand All @@ -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<Type, MethodInfo>();
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;
Expand Down