//--------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.Serialization; using System.Web.DynamicData; namespace System.Web.UI.WebControls { /// /// Represents errors that occur when validating properties of a dynamic data source. /// [Serializable] [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", Justification = "SerializeObjectState used instead")] public sealed class EntityDataSourceValidationException : Exception, IDynamicValidatorException { /// /// Exception state used to serialize/deserialize the exception in a safe manner. /// [NonSerialized] private EntityDataSourceValidationExceptionState _state; /// /// Initializes a new instance of the class. /// public EntityDataSourceValidationException() : base() { InitializeExceptionState(null); } /// /// Initializes a new instance of the class. /// /// Exception message. public EntityDataSourceValidationException(string message) : base(message) { InitializeExceptionState(null); } /// /// Initializes a new instance of the class. /// /// Exception message. /// Inner exception. public EntityDataSourceValidationException(string message, Exception innerException) : base(message, innerException) { InitializeExceptionState(null); } /// /// Initializes a new instance of the class. /// /// Exception message. /// Inner exceptions. internal EntityDataSourceValidationException(string message, Dictionary innerExceptions) : base(message) { InitializeExceptionState(innerExceptions); } /// /// Initializes internal exception state. /// /// Inner exceptions. private void InitializeExceptionState(Dictionary innerExceptions) { _state = new EntityDataSourceValidationExceptionState(innerExceptions); SubscribeToSerializeObjectState(); } /// /// Returns inner exceptions. /// IDictionary IDynamicValidatorException.InnerExceptions { get { return _state.InnerExceptions; } } /// /// Subscribes the SerializeObjectState event. /// private void SubscribeToSerializeObjectState() { SerializeObjectState += (exception, eventArgs) => eventArgs.AddSerializedState(_state); } /// /// Holds the exception state that will be serialized when the exception is serialized. /// [Serializable] private class EntityDataSourceValidationExceptionState : ISafeSerializationData { /// /// Inner exceptions. /// private readonly Dictionary _innerExceptions; /// /// Initializes a new instance of the class. /// /// public EntityDataSourceValidationExceptionState(Dictionary innerExceptions) { _innerExceptions = innerExceptions ?? new Dictionary(); } /// /// Returns inner exceptions. /// public Dictionary InnerExceptions { get { return _innerExceptions; } } /// /// Completes the deserialization. /// /// The deserialized object. public void CompleteDeserialization(object deserialized) { ((EntityDataSourceValidationException)deserialized)._state = this; } } } }