/// <summary> /// Creates the delegate. /// </summary> /// <typeparam name="TDelegate">The type of the delegate.</typeparam> /// <param name="creatorFunc">The creator function.</param> /// <param name="cacheStrategy">The cache strategy.</param> /// <returns>The delegate value</returns> private static object CreateDelegate <TDelegate>(Func <TDelegate> creatorFunc, DynamicMethodCacheStrategy cacheStrategy) { if (cacheStrategy == DynamicMethodCacheStrategy.Temporary) { return(new WeakReference(creatorFunc())); } return(creatorFunc()); }
/// <summary> /// Gets or adds the method delegate. /// </summary> /// <typeparam name="TDelegate">The type of the delegate.</typeparam> /// <param name="memberInfo">The member information.</param> /// <param name="creatorFunc">The creator function.</param> /// <param name="cacheStrategy">The cache strategy.</param> /// <returns>The method delegate.</returns> public TDelegate GetOrAddDelegate <TDelegate>(DynamicMethodInfo memberInfo, Func <TDelegate> creatorFunc, DynamicMethodCacheStrategy cacheStrategy) { object entry; if (m_Entries.TryGetValue(memberInfo, out entry)) { WeakReference weakReference = entry as WeakReference; if (weakReference != null) { if (weakReference.IsAlive) { return((TDelegate)weakReference.Target); } m_Entries[memberInfo] = entry = CreateDelegate(creatorFunc, cacheStrategy); } else { return((TDelegate)entry); } } else { m_Entries[memberInfo] = entry = CreateDelegate(creatorFunc, cacheStrategy); } return((TDelegate)(entry is WeakReference ? ((WeakReference)entry).Target : entry)); }