Exemple #1
0
        /// <summary>
        /// 获取当前表达式对应的动态执行方法。
        /// </summary>
        /// <param name="expression">当前条件表达式。</param>
        /// <param name="instance">通过对象执行当前表达式。</param>
        /// <returns>返回表达式代理方法。</returns>
        public static object Execute(string expression, object instance)
        {
            var type   = instance.GetType();
            var key    = expression + ":" + type.FullName;
            var lambda = _invokers.GetOrAdd(key, k =>
            {
                var templateExpression = new TemplateExpression(expression);
                return(templateExpression.Lambda(type));
            });
            var func = lambda.Compile();

            return(func.DynamicInvoke(instance));
        }
        /// <summary>
        /// 通过当前对象执行属性所得到的字符串。
        /// </summary>
        /// <param name="attribute">属性实例对象。</param>
        /// <param name="result">当前原有的属性列表。</param>
        /// <param name="instance">当前实例对象。</param>
        public void Execute(TemplateHtmlCodeAttribute attribute, Dictionary <string, string> result, object instance)
        {
            var value = attribute.Value?.Trim();

            if (!string.IsNullOrWhiteSpace(value))
            {
                var isTrue = (bool)TemplateExpression.Execute(value, instance);
                if (isTrue)
                {
                    result[attribute.AttributeName] = attribute[0].Trim('\'');
                }
                else if (attribute.Count == 2)
                {
                    result[attribute.AttributeName] = attribute[1].Trim('\'');
                }
            }
        }