Visual Guard Class Library Documentation

Using an expression in a condition of a security action

Visual Guard allows you to use an expression based on the Visual Guard expression language to dynamically determine whether an action should be executed or not. The Visual Guard expression language supports logical and arithmetic operators, method invocation and allows to access to the context of the security action.

Visual Guard evaluates the value of the expression at runtime for the current context of the security action and uses the return value to determine whether the action should be executed or not. The action is executed when the value is equal to true.

In this expression, you can use the following variables:

The root context of the expression corresponds to the target object on which the action is executed. The type of this context depends on the type of the target of the action. For example, when you define an action for a Windows form, the type of the variable is 'System.Windows.Forms.Form'. When you define an action for a WebForm page, the type is 'System.Web.UI.Page'.

For an action executed on a Windows Form, if the condition is equal to 'Text.Length > 0' the action will be executed only when the title of the form is not empty.

Evaluation of the expression

At runtime, the condition is evaluated just before executing the action. When the action is declared for a specific event, the value of the condition will be evaluated when the event is raised. Visual Guard resolves the type of the object graph at runtime. if the expression is not valid at runtime because an object is null or member does not exist, Visual Guard will throw an InvalidActionException exception .

Error on expression at design time

The Visual Guard security action designer tries to check the expression at design time. It parses the expression and evaluates the type of the objects statically declared.

In some cases, Visual Guard cannot evaluate the right type of an object and generates warning. For example, the expression 'Controls['MyCheckBox'].Checked' will generate a warning indicating that 'Checked' is not a property of the 'System.Windows.Forms.Control' class since the 'Controls[]' property returns a 'System.Windows.Forms.Control' object.

If you are sure that the type of the object returned by 'Controls['MyCheckBox']' is a checkbox at runtime, you can ignore this warning.

Examples of expression

The security action with the following condition will be executed only when _btnDelete button is enabled and the text of _txbCountry textbox is equal to 'USA'

_btnDelete.Enabled and _txbCountry.Text == 'USA'

For an action executed on the Closing event of a form, the following condition is true when the property 'Cancel of event argument is not equal to true

!#EventArgs.Cancel

See Also

Visual Guard expression language

How to apply security in your application