#if !SILVERLIGHT
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
#endif
namespace System.ComponentModel.DataAnnotations {
///
/// Exception used for validation using .
///
#if !SILVERLIGHT
[Serializable]
#endif
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", Justification = "SerializationInfo is internal in Silverlight")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2240:ImplementISerializableCorrectly",
Justification = "SecurityTransparent types cannot override GetObjectData, and this type does not serialize its instance fields anyway.")]
public class ValidationException : Exception {
private ValidationResult _validationResult;
///
/// Gets the ValidationAttribute instance that triggered this exception.
///
public ValidationAttribute ValidationAttribute { get; private set; }
///
/// Gets the instance that describes the validation error.
///
///
/// This property will never be null.
///
public ValidationResult ValidationResult {
get {
if (this._validationResult == null) {
this._validationResult = new ValidationResult(this.Message);
}
return this._validationResult;
}
}
///
/// Gets the value that caused the validating attribute to trigger the exception
///
public object Value { get; private set; }
///
/// Constructor that accepts a structured describing the problem.
///
/// The value describing the validation error
/// The attribute that triggered this exception
/// The value that caused the validating attribute to trigger the exception
public ValidationException(ValidationResult validationResult, ValidationAttribute validatingAttribute, object value)
: this(validationResult.ErrorMessage, validatingAttribute, value) {
this._validationResult = validationResult;
}
///
/// Constructor that accepts an error message, the failing attribute, and the invalid value.
///
/// The localized error message
/// The attribute that triggered this exception
/// The value that caused the validating attribute to trigger the exception
public ValidationException(string errorMessage, ValidationAttribute validatingAttribute, object value)
: base(errorMessage) {
this.Value = value;
this.ValidationAttribute = validatingAttribute;
}
//
///
/// Default constructor.
///
/// The long form of this constructor is preferred because it gives better error reporting.
public ValidationException()
: base() {
}
///
/// Constructor that accepts only a localized message
///
/// The long form of this constructor is preferred because it gives better error reporting.
/// The localized message
public ValidationException(string message)
: base(message) { }
///
/// Constructor that accepts a localized message and an inner exception
///
/// The long form of this constructor is preferred because it gives better error reporting
/// The localized error message
/// inner exception
public ValidationException(string message, Exception innerException)
: base(message, innerException) { }
#if !SILVERLIGHT // Does not have SerializationInfo (it is internal)
///
/// Constructor that takes serialization info
///
///
///
protected ValidationException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
#endif // !SILVERLIGHT
}
}