IRuleExpression 接口

定义

表示必须从中派生自定义表达式编写器以编写自定义表达式的基类。

public interface class IRuleExpression
public interface IRuleExpression
type IRuleExpression = interface
Public Interface IRuleExpression

示例

下面的代码创建可在声明条件和规则集中使用的表达式。 表达式名为 TwoOfThree,使用 3 个参数,所有这些参数的计算结果必须为布尔值。 如果 3 个表达式中的 2 个返回 true,则此表达式返回 true

若要使用此代码,请将它添加到类库项目并从工作流项目中引用该库。

using System.CodeDom;  
using System.Text;  
using System.Workflow.Activities.Rules;  
using System.Workflow.ComponentModel.Compiler;  

namespace TwoOfThreeRuleExpression  
{  
    public class TwoOfThree : CodeExpression, IRuleExpression  
    {  
        CodeExpression expression1, expression2, expression3;  

        public CodeExpression First  
        {  
            get { return expression1; }  
            set { expression1 = value; }  
        }  

        public CodeExpression Second  
        {  
            get { return expression2; }  
            set { expression2 = value; }  
        }  

        public CodeExpression Third  
        {  
            get { return expression3; }  
            set { expression3 = value; }  
        }  

        public TwoOfThree()  
        {  
            // constructor required for deserialization  
        }  

        public TwoOfThree(CodeExpression first, CodeExpression second, CodeExpression third)  
        {  
            // constructor required by parser  
            expression1 = first;  
            expression2 = second;  
            expression3 = third;  
        }  

        public void AnalyzeUsage(RuleAnalysis analysis, bool isRead, bool isWritten, RulePathQualifier qualifier)  
        {  
            // check what the 3 expressions use  
            RuleExpressionWalker.AnalyzeUsage(analysis, expression1, true, false, null);  
            RuleExpressionWalker.AnalyzeUsage(analysis, expression2, true, false, null);  
            RuleExpressionWalker.AnalyzeUsage(analysis, expression3, true, false, null);  
        }  

        public CodeExpression Clone()  
        {  
            TwoOfThree result = new TwoOfThree();  
            result.expression1 = RuleExpressionWalker.Clone(expression1);  
            result.expression2 = RuleExpressionWalker.Clone(expression2);  
            result.expression3 = RuleExpressionWalker.Clone(expression3);  
            return result;  
        }  

        public void Decompile(StringBuilder stringBuilder, CodeExpression parentExpression)  
        {  
            // what should be displayed by the parser  
            stringBuilder.Append("TwoOfThree(");  
            RuleExpressionWalker.Decompile(stringBuilder, expression1, this);  
            stringBuilder.Append(", ");  
            RuleExpressionWalker.Decompile(stringBuilder, expression2, this);  
            stringBuilder.Append(", ");  
            RuleExpressionWalker.Decompile(stringBuilder, expression3, this);  
            stringBuilder.Append(")");  
        }  

        static RuleLiteralResult resultTrue = new RuleLiteralResult(true);  
        static RuleLiteralResult resultFalse = new RuleLiteralResult(false);  

        public RuleExpressionResult Evaluate(RuleExecution execution)  
        {  
            // start by doing the first 2 expressions  
            RuleExpressionResult r1 = RuleExpressionWalker.Evaluate(execution, expression1);  
            RuleExpressionResult r2 = RuleExpressionWalker.Evaluate(execution, expression2);  
            bool b1 = (bool)r1.Value;  
            bool b2 = (bool)r2.Value;  
            if (b1 && b2)  
            {  
                // both are true, so result is true  
                return resultTrue;  
            }  
            else if (b1 || b2)  
            {  
                // only one of the first 2 is true, evaluate the third to determine result  
                return RuleExpressionWalker.Evaluate(execution, expression3);  
            }  
            else  
                // both e1 and e2 are false, so skip e3 and return false;  
                return resultFalse;  
        }  

        public bool Match(CodeExpression expression)  
        {  
            TwoOfThree other = expression as TwoOfThree;  
            return (other != null) &&  
                RuleExpressionWalker.Match(expression1, other.expression1) &&  
                RuleExpressionWalker.Match(expression2, other.expression2) &&  
                RuleExpressionWalker.Match(expression3, other.expression3);  
        }  

        public RuleExpressionInfo Validate(RuleValidation validation, bool isWritten)  
        {  
            ValidateExpression(validation, expression1, "First");  
            ValidateExpression(validation, expression2, "Second");  
            ValidateExpression(validation, expression3, "Third");  
            return new RuleExpressionInfo(typeof(bool));  
        }  

        private void ValidateExpression(RuleValidation validation, CodeExpression expression, string propertyName)  
        {  
            ValidationError error;  
            if (expression == null)  
            {  
                error = new ValidationError(propertyName + " cannot be null", 123);  
                validation.Errors.Add(error);  
            }  
            else  
            {  
                RuleExpressionInfo result = RuleExpressionWalker.Validate(validation, expression, false);  
                if ((result == null) || (result.ExpressionType != typeof(bool)))  
                {  
                    error = new ValidationError(propertyName + " must return boolean result", 123);  
                    validation.Errors.Add(error);  
                }  
            }  
        }  
    }  
}  

方法

AnalyzeUsage(RuleAnalysis, Boolean, Boolean, RulePathQualifier)

当在派生类中重写时,报告对象如何在上下文类型中使用字段和属性。

Clone()

在派生类中重写时,创建当前 CodeExpression 的深层副本。

Decompile(StringBuilder, CodeExpression)

在派生类中重写时,将自定义表达式反编译为字符串格式。

Evaluate(RuleExecution)

在派生类中重写时,计算自定义表达式。

Match(CodeExpression)

将当前表达式与另一个表达式相比较,以确定它们是否相等。

Validate(RuleValidation, Boolean)

在派生类中重写时,验证表达式是否配置正确且没有错误。

适用于