Details of Using Attributes

Details of Using Attributes

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

Validation attributes can be used with various targets that include classes, fields, properties, methods, and in limited cases parameters. These are discussed in Validation Targets. There is also a particular group of attributes that allow you to change the behavior of other attributes. These are discussed in Validation Modifier Attributes.

Validation Attribute Targets

Validation attributes can be used with various targets. The following table lists the targets and how the validators associated with the attributes behave.

Table 1: Attribute Targets

Target

Behavior of associated validator

Class

The validator checks the object. Typically, these attributes are custom type-level validators supplied by the user. The supplied validators, with the exception of the NonNullValidator, ObjectCollectionValidator, and Composite validators, can only be used at the member level and not the class level.

Field

The validator checks the value of the field.

Property

The validator checks the value of the get property.

Method

The validator checks the return value of the method. A validator attribute can only be applied to methods that take no arguments.

Parameter

Currently, the Validation Application Block ignores attributes attached to parameters except for WCF and the Policy Injection Application Block Validation Call Handler.

Validation Modifier Attributes

Validation modifier attributes allow you to change the way validation attributes behave. The validation modifier attributes are:

  • ValidatorComposition
  • IgnoreNulls
  • HasSelfValidation
  • SelfValidation

The ValidatorComposition attribute allows you to provide a composite validator as an attribute. With it, you can override the default behavior that occurs when you use more than one validator attribute. You can specify either CompositionType.And or CompositionType.Or as an argument to this attribute. The default composition type is AND. The default behavior is the same as if you do not use the attribute. Typically, you use the AND composition type when the logic is complex and requires OR operations as well as AND operations.

The IgnoreNulls attribute checks that the other validators will not be called if the value that you are checking is null.

The HasSelfValidation attribute marks a class that contains self-validation logic. The SelfValidation attribute marks self-validation methods within that class. For more information, see Using Self Validation.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.
Show:
© 2016 Microsoft