Beispiel #1
0
        /// <summary>
        /// Creates a new evaluator factory using the specified <paramref name="memoizer"/> to cache the
        /// compiled delegates created by <see cref="ShouldCache(MemberInfo)"/>. A weak memoizer is used which
        /// can help to prevent the cache from keeping member objects and evaluator delegates alive.
        /// </summary>
        /// <param name="memoizer">The memoizer to use for caching <see cref="GetEvaluator(MemberInfo)"/> calls.</param>
        public MemoizingEvaluatorFactory(IWeakMemoizer memoizer)
        {
            if (memoizer == null)
            {
                throw new ArgumentNullException(nameof(memoizer));
            }

            _getEvaluator = memoizer.MemoizeWeak <MemberInfo, Delegate>(base.GetEvaluator);
        }
        /// <summary>
        /// Memoizes the specified <paramref name="function"/> using the specified <paramref name="memoizer"/>.
        /// </summary>
        /// <typeparam name="TResult">Type of the function result.</typeparam>
        /// <param name="memoizer">The memoizer used to memoize the function.</param>
        /// <param name="function">The function to memoize.</param>
        /// <param name="options">Flags to influence the memoization behavior.</param>
        /// <returns>A memoized delegate containing the memoized function and providing access to the memoization cache.</returns>
        public static IMemoizedDelegate <Func <TResult> > MemoizeWeak <TResult>(this IWeakMemoizer memoizer, Func <TResult> function, MemoizationOptions options = MemoizationOptions.None)
        {
            if (memoizer == null)
            {
                throw new ArgumentNullException(nameof(memoizer));
            }
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            var res = memoizer.MemoizeWeak <object, TResult>(_ => function(), options);
            var del = res.Delegate;

            return(new MemoizedDelegate <Func <TResult> >(() => del(null), res.Cache));
        }