Пример #1
0
        /// <summary>
        /// Register the filters implemented by the provided System.Type entries so they can be used to resolve templates.
        /// </summary>
        /// <param name="implementingType"></param>
        /// <param name="filterName"></param>
        protected override void OnRegisterFilter(MethodInfo filterMethodInfo, string filterName)
        {
            // Define the DotLiquid-compatible function that will wrap the Lava filter.
            // When the wrapper function is executed by DotLiquid, it performs some necessary pre-processing before executing the Lava filter.
            Func <Context, List <object>, object> filterFunctionWrapper = (Context context, List <object> args) =>
            {
                var parameterInfos = filterMethodInfo.GetParameters();

                filterName = filterName ?? filterMethodInfo.Name;

                GetLavaFilterCompatibleArguments(filterName, args, parameterInfos, context);

                // Execute the static filter function and return the result.
                var result = filterMethodInfo.Invoke(null, args.ToArray());

                return(result);
            };

            // Register the filter with DotLiquid.
            Strainer.RegisterFilter(filterName, filterFunctionWrapper);
        }
Пример #2
0
        /// <summary>
        /// Register the filters implemented by the provided System.Type entries so they can be used to resolve templates.
        /// </summary>
        /// <param name="implementingType"></param>
        protected override void OnRegisterFilters(Type implementingType)
        {
            var methodsGroupedByName = implementingType.GetMethods(BindingFlags.Public | BindingFlags.Static).AsQueryable().GroupBy(k => k.Name, v => v);

            foreach (var methodGroup in methodsGroupedByName)
            {
                var filterName        = methodGroup.Key;
                var filterMethodInfos = methodGroup.OrderBy(m => m.GetParameters().Length).ToList();

                // Define the DotLiquid-compatible function that will wrap the Lava filter.
                // When the wrapper function is executed by DotLiquid, it performs some necessary pre-processing before executing the Lava filter.
                Func <Context, List <object>, object> filterFunctionWrapper = (Context context, List <object> args) =>
                {
                    // Get the filter method that best matches the provided argument list.
                    MethodInfo filterMethodInfo = null;

                    if (filterMethodInfos.Count == 1)
                    {
                        filterMethodInfo = filterMethodInfos.First();
                    }
                    else
                    {
                        // Find the method that best matches the provided list of arguments.
                        filterMethodInfo = GetMatchedFilterFunction(filterMethodInfos, args.Count);
                    }

                    var parameterInfos = filterMethodInfo.GetParameters();

                    GetLavaFilterCompatibleArguments(filterName, args, parameterInfos, context);

                    // Execute the static filter function and return the result.
                    var result = filterMethodInfo.Invoke(null, args.ToArray());

                    return(result);
                };

                // Register the set of filters for each method name.
                Strainer.RegisterFilter(methodGroup.Key, filterFunctionWrapper);
            }
        }