ActivityValidator.Validate(ValidationManager, Object) Method

Definition

Verifies that the given activity is valid.

public override System.Workflow.ComponentModel.Compiler.ValidationErrorCollection Validate(System.Workflow.ComponentModel.Compiler.ValidationManager manager, object obj);

Parameters

manager
ValidationManager

The ValidationManager associated with validation.

obj
Object

The Activity to be validated.

Returns

A ValidationErrorCollection object that contains any errors or warnings that occurred during validation.

Examples

The following example shows how to override the Validate method for an ActivityValidator used for a custom activity that has a single dependency property named Msg of type String. The custom validator ensures that the Msg property is set. If it is not set, the compiler displays an error when the Validate method is called on the ActivityValidator and the compilation fails.

public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
{
    // Invoke the base class method implementation to
    // perform default validation.
    ValidationErrorCollection errors = base.Validate(manager, obj);

    // Make sure there is an activity instance.
    ConsoleWriteLineActivity crw = obj as ConsoleWriteLineActivity;
    if (crw == null)
    {
        throw new InvalidOperationException();
    }

    // If the activity has no parent then this validation
    // is occurring during the compilation of the activity
    // and not during the hosting or creation of an
    // activity instance.
    if (crw.Parent == null)
    {
        // Can skip the rest of the validation because
        // it deals with the hosting and the creation
        // of the activity.
        return errors;
    }

    // Msg is required. Add a validation error if there is no
    // Msg specified or Msg is not bound to another property.
    if (string.IsNullOrEmpty(crw.Msg) &&
        crw.GetBinding(ConsoleWriteLineActivity.MsgProperty) == null)
    {
        errors.Add(new ValidationError("Msg is required", 100, false, "Msg"));
    }

    return errors;
}

Remarks

This method calls ValidateProperties to validate the properties of the Activity.

This method only validates the uniqueness of the Name for the Activity if the Activity is not a root Activity.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1