using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; namespace System.ComponentModel.DataAnnotations { /// /// Container class for the results of a validation request. /// /// Use the static to represent successful validation. /// /// /// public sealed class ValidationResult { #region Member Fields private IEnumerable _memberNames; private string _errorMessage; /// /// Gets a that indicates Success. /// /// /// The null value is used to indicate success. Consumers of s /// should compare the values to rather than checking for null. /// [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "We want this to be readonly since we're just returning null")] public static readonly ValidationResult Success; #endregion #region All Constructors /// /// Constructor that accepts an error message. This error message would override any error message /// provided on the . /// /// The user-visible error message. If null, /// will use for its error message. public ValidationResult(string errorMessage) : this(errorMessage, null) { } /// /// Constructor that accepts an error message as well as a list of member names involved in the validation. /// This error message would override any error message provided on the . /// /// The user-visible error message. If null, /// will use for its error message. /// The list of member names affected by this result. /// This list of member names is meant to be used by presentation layers to indicate which fields are in error. public ValidationResult(string errorMessage, IEnumerable memberNames) { this._errorMessage = errorMessage; this._memberNames = memberNames ?? new string[0]; } #endregion #region Properties /// /// Gets the collection of member names affected by this result. The collection may be empty but will never be null. /// public IEnumerable MemberNames { get { return this._memberNames; } } /// /// Gets the error message for this result. It may be null. /// public string ErrorMessage { get { return this._errorMessage; } set { this._errorMessage = value; } } #endregion #region Methods /// /// Override the string representation of this instance, returning /// the if not null, otherwise /// the base result. /// /// /// If the is empty, it will still qualify /// as being specified, and therefore returned from . /// /// The property value if specified, /// otherwise, the base result. public override string ToString() { return this.ErrorMessage ?? base.ToString(); } #endregion Methods } }