Partilhar via


DataTypeAttribute Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Specifies the name of an additional type to associate with an entity member.

Inheritance Hierarchy

System.Object
  System.Attribute
    System.ComponentModel.DataAnnotations.ValidationAttribute
      System.ComponentModel.DataAnnotations.DataTypeAttribute
        System.ComponentModel.DataAnnotations.EnumDataTypeAttribute

Namespace:  System.ComponentModel.DataAnnotations
Assembly:  System.ComponentModel.DataAnnotations (in System.ComponentModel.DataAnnotations.dll)

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Method Or AttributeTargets.Property Or AttributeTargets.Field Or AttributeTargets.Parameter, AllowMultiple := False)> _
Public Class DataTypeAttribute _
    Inherits ValidationAttribute
[AttributeUsageAttribute(AttributeTargets.Method|AttributeTargets.Property|AttributeTargets.Field|AttributeTargets.Parameter, AllowMultiple = false)]
public class DataTypeAttribute : ValidationAttribute

The DataTypeAttribute type exposes the following members.

Constructors

  Name Description
Public method DataTypeAttribute(DataType) Initializes a new instance of the DataTypeAttribute class with the specified data type enumeration.
Public method DataTypeAttribute(String) Initializes a new instance of the DataTypeAttribute class with the specified name of a customized data type.

Top

Properties

  Name Description
Public property CustomDataType Gets a string that represents the name of the customized data type to associate with the member.
Public property DataType Gets the type of the data object to associate with the member.
Public property DisplayFormat Gets the object that represents the format of the data type.
Public property ErrorMessage Gets or sets the non-localizable error message to display when validation fails. (Inherited from ValidationAttribute.)
Public property ErrorMessageResourceName Gets or sets the property name on the resource type that provides the localizable error message. (Inherited from ValidationAttribute.)
Public property ErrorMessageResourceType Gets or sets the resource type that provides the localizable error message. (Inherited from ValidationAttribute.)
Protected property ErrorMessageString Gets the localized or non-localized error message. (Inherited from ValidationAttribute.)

Top

Methods

  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method FormatErrorMessage Applies formatting to the error message. (Inherited from ValidationAttribute.)
Public method GetDataTypeName Retrieves the name of the data type that is associated with the member.
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method GetValidationResult Determines whether the specified object is valid and returns an object that includes the results of the validation check. (Inherited from ValidationAttribute.)
Protected method IsValid Determines whether the specified object is valid. (Inherited from ValidationAttribute.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Validate Determines whether the specified object is valid and throws a ValidationException if the object is not valid. (Inherited from ValidationAttribute.)

Top

Remarks

You use the DataType enumeration when you apply the DataTypeAttribute attribute to a property. The DataType enumeration contains valid values you use with the DataTypeAttribute attribute. You use the DataTypeAttribute attribute to specify the type of data that is expected for the property beyond the data type of the property. For example, properties named Phone and EmailAddress may both contain string values, but you can specify the expected values by applying the DataTypeAttribute attribute with the PhoneNumber and EmailAddress values respectively.

Examples

The following example shows how to specify an e-mail address property and a phone number property.

Public Class Customer
  <DataType(DataType.EmailAddress)> _
  Public Property EmailAddress As String
    'Implement Get and Set logic
  End Property

  <DataType(DataType.PhoneNumber)> _
  Public Property Phone As String
    'Implement Get and Set logic
  End Property
End Class
public class Customer
{
  [DataType(DataType.EmailAddress)]
  public string EmailAddress { get; set; }

  [DataType(DataType.PhoneNumber)]
  public string Phone { get; set; }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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.