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.10.0 (in Microsoft.VisualStudio.Modeling.Sdk.10.0.dll)
Syntax
声明
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 | |
---|---|---|
ValidationContext(array<String[], ModelElement) | Initializes a new instance of the ValidationContext class that has a specific model element to validate. | |
ValidationContext(array<String[], IEnumerable<ModelElement>) | Ctor | |
ValidationContext(ValidationCategories, ModelElement) | Constructor | |
ValidationContext(ValidationCategories, IEnumerable<ModelElement>) | Constructor |
Top
Properties
Name | Description | |
---|---|---|
Categories | Gets the validation categories for this validation context. | |
CurrentViolations | Gets the collection of validation messages for the validation context. | |
CustomCategories | Gets the custom validation strings for the validation context. | |
ValidationSubjects | Gets the list of model elements to validate. |
Top
Methods
Name | Description | |
---|---|---|
ConstructValidationMessage | Constructs a validation message. You can override this method to construct a custom validation message. | |
Equals | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetCache<T>() | Gets the cache for the specified class. | |
GetCache<T>(String) | Gets the cache for the specified class. | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetNavigationProxyModelElements | Gets the proxy model elements when a validation error occurs in the model. | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
LogError | Creates a validation error and logs a message into the collection that the validation context maintains. | |
LogFatal | Creates a fatal error for validation and logs a message in the validation context. | |
LogMessage | Creates a validation information message and logs it into the collection that the validation context maintains. | |
LogViolation | Creates a validation error, message, or warning. | |
LogWarning | Creates a validation warning and logs the message into the collection that the validation context maintains. | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
SetCacheValue<T> | Set the cached object associated with the name | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
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.