Implement delegate caching

This commit is contained in:
Brant Martin
2019-02-10 19:14:15 -05:00
parent 1c6eec61af
commit ac672092f1

View File

@@ -107,10 +107,18 @@ namespace Torch.Managers
} }
#region Network Injection #region Network Injection
private static Dictionary<MethodInfo, Delegate> _delegateCache = new Dictionary<MethodInfo, Delegate>();
private static Func<T, TA> GetDelegate<T, TA>(MethodInfo method) where TA : class private static Func<T, TA> GetDelegate<T, TA>(MethodInfo method) where TA : class
{ {
return x => Delegate.CreateDelegate(typeof(TA), x, method) as TA; if (!_delegateCache.TryGetValue(method, out var del))
{
del = (Func<T, TA>)(x => Delegate.CreateDelegate(typeof(TA), x, method) as TA);
_delegateCache[method] = del;
}
return (Func<T, TA>)del;
} }
public static void RaiseEvent<T1>(T1 instance, MethodInfo method, EndpointId target = default(EndpointId)) where T1 : IMyEventOwner public static void RaiseEvent<T1>(T1 instance, MethodInfo method, EndpointId target = default(EndpointId)) where T1 : IMyEventOwner