Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web.Entity / System / Data / WebControls / EntityDataSourceValidationException.cs
1 //---------------------------------------------------------------------
2 // <copyright file="EntityDataSourceValidationException.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9 using System.Collections.Generic;
10 using System.Diagnostics.CodeAnalysis;
11 using System.Runtime.Serialization;
12 using System.Web.DynamicData;
13
14 namespace System.Web.UI.WebControls
15 {
16     /// <summary>
17     /// Represents errors that occur when validating properties of a dynamic data source.
18     /// </summary>
19     [Serializable]
20     [SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors", Justification = "SerializeObjectState used instead")]
21     public sealed class EntityDataSourceValidationException : Exception, IDynamicValidatorException
22     {
23         /// <summary>
24         /// Exception state used to serialize/deserialize the exception in a safe manner.
25         /// </summary>
26         [NonSerialized]
27         private EntityDataSourceValidationExceptionState _state;
28
29         /// <summary>
30         /// Initializes a new instance of the <see cref="EntityDataSourceValidationException" /> class.
31         /// </summary>
32         public EntityDataSourceValidationException()
33             : base()
34         {
35             InitializeExceptionState(null);
36         }
37
38         /// <summary>
39         /// Initializes a new instance of the <see cref="EntityDataSourceValidationException" /> class.
40         /// </summary>
41         /// <param name="message">Exception message.</param>
42         public EntityDataSourceValidationException(string message)
43             : base(message)
44         {
45             InitializeExceptionState(null);
46         }
47
48         /// <summary>
49         /// Initializes a new instance of the <see cref="EntityDataSourceValidationException" /> class.
50         /// </summary>
51         /// <param name="message">Exception message.</param>
52         /// <param name="innerException">Inner exception.</param>
53         public EntityDataSourceValidationException(string message, Exception innerException)
54             : base(message, innerException)
55         {
56             InitializeExceptionState(null);
57         }
58
59         /// <summary>
60         /// Initializes a new instance of the <see cref="EntityDataSourceValidationException" /> class.
61         /// </summary>
62         /// <param name="message">Exception message.</param>
63         /// <param name="innerExceptions">Inner exceptions.</param>
64         internal EntityDataSourceValidationException(string message, Dictionary<string, Exception> innerExceptions)
65             : base(message)
66         {
67             InitializeExceptionState(innerExceptions);
68         }
69
70         /// <summary>
71         /// Initializes internal exception state.
72         /// </summary>
73         /// <param name="innerExceptions">Inner exceptions.</param>
74         private void InitializeExceptionState(Dictionary<string, Exception> innerExceptions)
75         {
76             _state = new EntityDataSourceValidationExceptionState(innerExceptions);
77             SubscribeToSerializeObjectState();
78         }
79
80         /// <summary>
81         /// Returns inner exceptions.
82         /// </summary>
83         IDictionary<string, Exception> IDynamicValidatorException.InnerExceptions
84         {
85             get { return _state.InnerExceptions; }
86         }
87
88         /// <summary>
89         /// Subscribes the SerializeObjectState event.
90         /// </summary>
91         private void SubscribeToSerializeObjectState()
92         {
93             SerializeObjectState += (exception, eventArgs) => eventArgs.AddSerializedState(_state);
94         }
95
96         /// <summary>
97         /// Holds the exception state that will be serialized when the exception is serialized.
98         /// </summary>
99         [Serializable]
100         private class EntityDataSourceValidationExceptionState : ISafeSerializationData
101         {
102             /// <summary>
103             /// Inner exceptions.
104             /// </summary>
105             private readonly Dictionary<string, Exception> _innerExceptions;
106
107             /// <summary>
108             /// Initializes a new instance of the <see cref="EntityDataSourceValidationExceptionState"/> class.
109             /// </summary>
110             /// <param name="innerExceptions"></param>
111             public EntityDataSourceValidationExceptionState(Dictionary<string, Exception> innerExceptions)
112             {
113                 _innerExceptions = innerExceptions ?? new Dictionary<string, Exception>();
114             }
115
116             /// <summary>
117             /// Returns inner exceptions.
118             /// </summary>
119             public Dictionary<string, Exception> InnerExceptions
120             {
121                 get
122                 {
123                     return _innerExceptions;
124                 }
125             }
126
127             /// <summary>
128             /// Completes the deserialization.
129             /// </summary>
130             /// <param name="deserialized">The deserialized object.</param>
131             public void CompleteDeserialization(object deserialized)
132             {
133                 ((EntityDataSourceValidationException)deserialized)._state = this;
134             }
135         }
136     }
137 }