Windows Dev Center

Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

CustomValidationAttribute Class

Specifies a custom validation method that is used to validate a property or class instance.

System.Object
  System.Attribute
    System.ComponentModel.DataAnnotations.ValidationAttribute
      System.ComponentModel.DataAnnotations.CustomValidationAttribute

Namespace:  System.ComponentModel.DataAnnotations
Assemblies:   System.ComponentModel.Annotations (in System.ComponentModel.Annotations.dll)
  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = true)]
public sealed class CustomValidationAttribute : ValidationAttribute

The CustomValidationAttribute type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibraryCustomValidationAttributeInitializes a new instance of the CustomValidationAttribute class.
Top

  NameDescription
Public propertySupported by Portable Class LibraryErrorMessageGets or sets an error message to associate with a validation control if validation fails. (Inherited from ValidationAttribute.)
Public propertySupported by Portable Class LibraryErrorMessageResourceNameGets or sets the error message resource name to use in order to look up the ErrorMessageResourceType property value if validation fails. (Inherited from ValidationAttribute.)
Public propertySupported by Portable Class LibraryErrorMessageResourceTypeGets or sets the resource type to use for error-message lookup if validation fails. (Inherited from ValidationAttribute.)
Public propertySupported by Portable Class LibraryMethodGets the validation method.
Public propertyRequiresValidationContextGets a value that indicates whether the attribute requires validation context. (Inherited from ValidationAttribute.)
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public propertyTypeIdGets a unique identifier for this attribute. (Overrides Attribute.TypeId.)
Public propertySupported by Portable Class LibraryValidatorTypeGets the type that performs custom validation.
Top

  NameDescription
Public methodSupported by Portable Class LibraryEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public methodSupported by Portable Class LibraryFormatErrorMessageFormats a validation error message. (Overrides ValidationAttribute.FormatErrorMessage(String).)
Public methodSupported by Portable Class LibraryGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetValidationResultChecks whether the specified value is valid with respect to the current validation attribute. (Inherited from ValidationAttribute.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodIsValid(Object)Determines whether the specified value of the object is valid. (Inherited from ValidationAttribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public methodSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryValidate(Object, ValidationContext)Validates the specified object. (Inherited from ValidationAttribute.)
Public methodValidate(Object, String)Validates the specified object. (Inherited from ValidationAttribute.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

The CustomValidationAttribute attribute is used to perform custom validation when the IsValidmethod is invoked to perform validation. The IsValid method then redirects the call to the method that is identified by the Method property, which in turn performs the actual validation.

The CustomValidationAttribute attribute can be applied to types, properties, fields, methods, and method parameters. When it is applied to a property, the attribute is invoked whenever a value is assigned to that property. When it is applied to a method, the attribute is invoked whenever the program calls that method. When it is applied to a method parameter, the attribute is invoked before the method is called.

For more information about using attributes, see Extending Metadata Using Attributes.

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft