RuleAction Class (System.Workflow.Activities.Rules)

Switch View :
ScriptFree
.NET Framework Class Library
RuleAction Class

Represents an abstract class that defines an action to be executed if the associated Condition evaluates to true, for ThenActions, or false, for ElseActions. This class must be inherited.

Inheritance Hierarchy

System.Object
  System.Workflow.Activities.Rules.RuleAction
    System.Workflow.Activities.Rules.RuleHaltAction
    System.Workflow.Activities.Rules.RuleStatementAction
    System.Workflow.Activities.Rules.RuleUpdateAction

Namespace:  System.Workflow.Activities.Rules
Assembly:  System.Workflow.Activities (in System.Workflow.Activities.dll)
Syntax

Visual Basic
<SerializableAttribute> _
Public MustInherit Class RuleAction
C#
[SerializableAttribute]
public abstract class RuleAction
Visual C++
[SerializableAttribute]
public ref class RuleAction abstract
F#
[<AbstractClass>]
[<SerializableAttribute>]
type RuleAction =  class end

The RuleAction type exposes the following members.

Constructors

  Name Description
Protected method RuleAction When implemented in a derived class, initializes a new instance of the RuleAction class.
Top
Methods

  Name Description
Public method Clone Creates a deep copy of the current RuleAction.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Public method Execute Executes the RuleAction using the specified RuleExecution instance.
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetSideEffects Returns the fields and properties updated by a RuleAction.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Validate Verifies that the RuleAction is configured correctly and has no errors.
Top
Remarks

RuleStatementAction objects (which can be used as both ThenActions and ElseActions) typically set a variable value on one of the properties of the activity, call a method of the activity, or call static methods on types in referenced assemblies.

RuleAction is the base type that RuleStatementAction, RuleHaltAction, and RuleUpdateAction classes derive from. The uses of these classes is as follows:

  • A RuleStatementAction modifies a property or calls a method.

  • A RuleHaltAction causes the RuleSet to stop executing and returns control to the calling method.

  • A RuleUpdateAction explicitly indicates that a rule is updating a variable. This causes the re-evaluation of any affected rules.

Examples

The following code creates an action that can be used in rule sets. The action is named Log, and takes a single parameter, which must evaluate to a string. This action outputs the string to the Console.

To use this code, add it to a Class Library project and reference the library from your workflow project.

C#
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Activities.Rules;
using System.Workflow.ComponentModel.Compiler;

namespace LogRuleAction
{
    public class Log : RuleAction
    {
        CodeExpression message;

        public CodeExpression Message
        {
            get { return message; }
            set { message = value; }
        }

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

        public Log(CodeExpression expression)
        {
            // constructor required by parser
            message = expression;
        }

        public override bool Validate(RuleValidation validator)
        {
            ValidationError error;
            if (message == null)
            {
                error = new ValidationError("Message cannot be null", 123);
                validator.Errors.Add(error);
                return false;
            }
            else
            {
                RuleExpressionInfo result = RuleExpressionWalker.Validate(validator, message, false);
                if ((result == null) || (result.ExpressionType != typeof(string)))
                {
                    error = new ValidationError("Message must return string result", 123);
                    validator.Errors.Add(error);
                    return false;
                }
            }
            return (validator.Errors.Count == 0);
        }

        public override RuleAction Clone()
        {
            Log result = new Log();
            result.Message = RuleExpressionWalker.Clone(message);
            return result;
        }

        public override void Execute(RuleExecution context)
        {
            RuleExpressionResult result = RuleExpressionWalker.Evaluate(context, message);
            if (result != null)
                Console.WriteLine(result.Value);
        }

        public override ICollection<string> GetSideEffects(RuleValidation validation)
        {
            RuleAnalysis analysis = new RuleAnalysis(validation, true);
            if (message != null)
                RuleExpressionWalker.AnalyzeUsage(analysis, message, true, false, null);
            return analysis.GetSymbols();
        }

        public override string ToString()
        {
            // what should be displayed by the parser
            StringBuilder result = new StringBuilder("Log(");
            RuleExpressionWalker.Decompile(result, message, null);
            result.Append(")");
            return result.ToString();
        }
    }
}
Version Information

.NET Framework

Supported in: 4, 3.5, 3.0
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference