Partager via


ValidationContext Class

Contains information about the current validation processing being performed.

Inheritance Hierarchy

System.Object
  Microsoft.VisualStudio.Modeling.Validation.ValidationContext
    Microsoft.VisualStudio.Modeling.Shell.VsValidationContext

Namespace:  Microsoft.VisualStudio.Modeling.Validation
Assembly:  Microsoft.VisualStudio.Modeling.Sdk.12.0 (in Microsoft.VisualStudio.Modeling.Sdk.12.0.dll)

Syntax

'Declaration
Public Class ValidationContext
public class ValidationContext
public ref class ValidationContext
type ValidationContext =  class end
public class ValidationContext

The ValidationContext type exposes the following members.

Constructors

  Name Description
Public method ValidationContext(array<String[], ModelElement) Initializes a new instance of the ValidationContext class that has a specific model element to validate.
Public method ValidationContext(array<String[], IEnumerable<ModelElement>) Ctor
Public method ValidationContext(ValidationCategories, ModelElement) Constructor
Public method ValidationContext(ValidationCategories, IEnumerable<ModelElement>) Constructor

Top

Properties

  Name Description
Public property Categories Gets the validation categories for this validation context.
Public property CurrentViolations Gets the collection of validation messages for the validation context.
Public property CustomCategories Gets the custom validation strings for the validation context.
Public property ValidationSubjects Gets the list of model elements to validate.

Top

Methods

  Name Description
Protected method ConstructValidationMessage Constructs a validation message. You can override this method to construct a custom validation message.
Public method Equals Determines whether the specified object is equal to the current object. (Inherited from Object.)
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 GetCache<T>() Gets the cache for the specified class.
Public method GetCache<T>(String) Gets the cache for the specified class.
Public method GetHashCode Serves as the default hash function. (Inherited from Object.)
Public method GetNavigationProxyModelElements Gets the proxy model elements when a validation error occurs in the model.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method LogError Creates a validation error and logs a message into the collection that the validation context maintains.
Public method LogFatal Creates a fatal error for validation and logs a message in the validation context.
Public method LogMessage Creates a validation information message and logs it into the collection that the validation context maintains.
Public method LogViolation Creates a validation error, message, or warning.
Public method LogWarning Creates a validation warning and logs the message into the collection that the validation context maintains.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetCacheValue<T> Set the cached object associated with the name
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method TryGetCacheValue<T> Returns whether the cached object associated with the name exist or not

Top

Remarks

An instance of this class is created every time that the modeling namespace starts the validation checking. This instance is passed into each validation method that you have registered for validation checking.

Each time that a validation method that you write is called, your code can log validation errors by using the LogError, LogWarning, and LogMessage methods. These validation errors are added to the CurrentViolations property of the ValidationContext object.

When the validation is finished, all validation errors, warnings, and messages are represented as collections of LogMessage objects in the CurrentViolations property.

The ValidationContext object is then passed to any subsequent validation methods. The CurrentViolations property contains all the errors, warnings, and messages that were encountered up to that point in the current validation checking.

The next time that validation starts, another ValidationContext object is created. That object is passed to each validation method in turn with errors, warnings, and messages added to that object as they are encountered.

For more information, see Validation in a Domain-Specific Language.

Examples

The following example is a method that is decorated by an attribute that indicates that it is a validation method.

The ValidationContext object that is passed into each validation method contains information about the current validation processing. This information includes the errors, warnings, and messages that accumulated in the validation methods that have already run.

The ValidationContext object has methods that add to the errors, warnings, and messages, such as the LogError method in the following example:

[ValidationMethod
(
    ValidationCategory.Open |
    ValidationCategory.Save |
    ValidationCategory.Menu
)
]
private void ValidateParentBirth(ValidationContext context)
{
    foreach (Person parent in this.Parent)
        {
        if (this.Birth <= parent.Birth)
        {
            context.LogError(
                       "Birth must be after Parent's birth",
                       "FamilyParentBirthError", 
                       this, 
                       parent);
        }
      }
}

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

Microsoft.VisualStudio.Modeling.Validation Namespace