/// <summary> /// Gets a dispatcher from the cache if available otherwise /// invokes factory to produce one and then cache it. /// </summary> /// <param name="key">the dispatcher key</param> /// <param name="factory">function to produce a dispatcher in case one it's not yet available</param> /// <returns></returns> public Dispatcher Get(DispatcherKey key, DispatcherFactory factory) { Dispatcher dispatcher; if (!_cache.TryGetValue(key, out dispatcher)) { lock (_cache) { if (!_cache.TryGetValue(key, out dispatcher)) { dispatcher = factory(); _cache.Add(key, dispatcher); } } } return dispatcher; }
private static Dispatcher GetDispatcher(object target, string cacheKeyName, Type[] cacheKeyTypes, DispatcherCache.DispatcherFactory factory) { var targetType = (target as Type) ?? target.GetType(); var key = new DispatcherKey(targetType, cacheKeyName, cacheKeyTypes); return _cache.Get(key, factory); }
private static object Dispatch(object target, string cacheKeyName, Type[] cacheKeyTypes, object[] args, DispatcherCache.DispatcherFactory factory) { Type targetType = (target as Type) ?? target.GetType(); DispatcherKey key = new DispatcherKey(targetType, cacheKeyName, cacheKeyTypes); Dispatcher dispatcher = _cache.Get(key, factory); return dispatcher(target, args); }