Share via


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. For information, see Validation Attribute Targets. There is also a set 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 describes how the validators associated with the attributes behave.

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

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 the following:

  • 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 specifies 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.