mscorlib(4.0.0.0) API with additions
AggregateException.cs
3 using System.Diagnostics;
7 using System.Security;
8 
9 namespace System
10 {
12  [Serializable]
13  [DebuggerDisplay("Count = {InnerExceptionCount}")]
14  [__DynamicallyInvokable]
16  {
17  private ReadOnlyCollection<Exception> m_innerExceptions;
18 
21  [__DynamicallyInvokable]
23  {
24  [__DynamicallyInvokable]
25  get
26  {
27  return m_innerExceptions;
28  }
29  }
30 
31  private int InnerExceptionCount => InnerExceptions.Count;
32 
34  [__DynamicallyInvokable]
36  : base(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"))
37  {
38  m_innerExceptions = new ReadOnlyCollection<Exception>(new Exception[0]);
39  }
40 
43  [__DynamicallyInvokable]
44  public AggregateException(string message)
45  : base(message)
46  {
47  m_innerExceptions = new ReadOnlyCollection<Exception>(new Exception[0]);
48  }
49 
54  [__DynamicallyInvokable]
55  public AggregateException(string message, Exception innerException)
56  : base(message, innerException)
57  {
58  if (innerException == null)
59  {
60  throw new ArgumentNullException("innerException");
61  }
62  m_innerExceptions = new ReadOnlyCollection<Exception>(new Exception[1]
63  {
64  innerException
65  });
66  }
67 
72  [__DynamicallyInvokable]
74  : this(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions)
75  {
76  }
77 
82  [__DynamicallyInvokable]
83  public AggregateException(params Exception[] innerExceptions)
84  : this(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptions)
85  {
86  }
87 
93  [__DynamicallyInvokable]
94  public AggregateException(string message, IEnumerable<Exception> innerExceptions)
95  : this(message, (innerExceptions as IList<Exception>) ?? ((innerExceptions == null) ? null : new List<Exception>(innerExceptions)))
96  {
97  }
98 
104  [__DynamicallyInvokable]
105  public AggregateException(string message, params Exception[] innerExceptions)
106  : this(message, (IList<Exception>)innerExceptions)
107  {
108  }
109 
110  private AggregateException(string message, IList<Exception> innerExceptions)
111  : base(message, (innerExceptions != null && innerExceptions.Count > 0) ? innerExceptions[0] : null)
112  {
113  if (innerExceptions == null)
114  {
115  throw new ArgumentNullException("innerExceptions");
116  }
117  Exception[] array = new Exception[innerExceptions.Count];
118  for (int i = 0; i < array.Length; i++)
119  {
120  array[i] = innerExceptions[i];
121  if (array[i] == null)
122  {
123  throw new ArgumentException(Environment.GetResourceString("AggregateException_ctor_InnerExceptionNull"));
124  }
125  }
126  m_innerExceptions = new ReadOnlyCollection<Exception>(array);
127  }
128 
129  internal AggregateException(IEnumerable<ExceptionDispatchInfo> innerExceptionInfos)
130  : this(Environment.GetResourceString("AggregateException_ctor_DefaultMessage"), innerExceptionInfos)
131  {
132  }
133 
134  internal AggregateException(string message, IEnumerable<ExceptionDispatchInfo> innerExceptionInfos)
135  : this(message, (innerExceptionInfos as IList<ExceptionDispatchInfo>) ?? ((innerExceptionInfos == null) ? null : new List<ExceptionDispatchInfo>(innerExceptionInfos)))
136  {
137  }
138 
139  private AggregateException(string message, IList<ExceptionDispatchInfo> innerExceptionInfos)
140  : base(message, (innerExceptionInfos != null && innerExceptionInfos.Count > 0 && innerExceptionInfos[0] != null) ? innerExceptionInfos[0].SourceException : null)
141  {
142  if (innerExceptionInfos == null)
143  {
144  throw new ArgumentNullException("innerExceptionInfos");
145  }
146  Exception[] array = new Exception[innerExceptionInfos.Count];
147  for (int i = 0; i < array.Length; i++)
148  {
149  ExceptionDispatchInfo exceptionDispatchInfo = innerExceptionInfos[i];
150  if (exceptionDispatchInfo != null)
151  {
152  array[i] = exceptionDispatchInfo.SourceException;
153  }
154  if (array[i] == null)
155  {
156  throw new ArgumentException(Environment.GetResourceString("AggregateException_ctor_InnerExceptionNull"));
157  }
158  }
159  m_innerExceptions = new ReadOnlyCollection<Exception>(array);
160  }
161 
167  [SecurityCritical]
169  : base(info, context)
170  {
171  if (info == null)
172  {
173  throw new ArgumentNullException("info");
174  }
175  Exception[] array = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[];
176  if (array == null)
177  {
178  throw new SerializationException(Environment.GetResourceString("AggregateException_DeserializationFailure"));
179  }
180  m_innerExceptions = new ReadOnlyCollection<Exception>(array);
181  }
182 
187  [SecurityCritical]
188  public override void GetObjectData(SerializationInfo info, StreamingContext context)
189  {
190  if (info == null)
191  {
192  throw new ArgumentNullException("info");
193  }
194  base.GetObjectData(info, context);
195  Exception[] array = new Exception[m_innerExceptions.Count];
196  m_innerExceptions.CopyTo(array, 0);
197  info.AddValue("InnerExceptions", array, typeof(Exception[]));
198  }
199 
202  [__DynamicallyInvokable]
203  public override Exception GetBaseException()
204  {
205  Exception ex = this;
206  AggregateException ex2 = this;
207  while (ex2 != null && ex2.InnerExceptions.Count == 1)
208  {
209  ex = ex.InnerException;
210  ex2 = (ex as AggregateException);
211  }
212  return ex;
213  }
214 
219  [__DynamicallyInvokable]
220  public void Handle(Func<Exception, bool> predicate)
221  {
222  if (predicate == null)
223  {
224  throw new ArgumentNullException("predicate");
225  }
226  List<Exception> list = null;
227  for (int i = 0; i < m_innerExceptions.Count; i++)
228  {
229  if (!predicate(m_innerExceptions[i]))
230  {
231  if (list == null)
232  {
233  list = new List<Exception>();
234  }
235  list.Add(m_innerExceptions[i]);
236  }
237  }
238  if (list != null)
239  {
240  throw new AggregateException(Message, list);
241  }
242  }
243 
246  [__DynamicallyInvokable]
248  {
249  List<Exception> list = new List<Exception>();
251  list2.Add(this);
252  int num = 0;
253  while (list2.Count > num)
254  {
255  IList<Exception> innerExceptions = list2[num++].InnerExceptions;
256  for (int i = 0; i < innerExceptions.Count; i++)
257  {
258  Exception ex = innerExceptions[i];
259  if (ex != null)
260  {
262  if (ex2 != null)
263  {
264  list2.Add(ex2);
265  }
266  else
267  {
268  list.Add(ex);
269  }
270  }
271  }
272  }
273  return new AggregateException(Message, list);
274  }
275 
278  [__DynamicallyInvokable]
279  public override string ToString()
280  {
281  string text = base.ToString();
282  for (int i = 0; i < m_innerExceptions.Count; i++)
283  {
284  text = string.Format(CultureInfo.InvariantCulture, Environment.GetResourceString("AggregateException_ToString"), text, Environment.NewLine, i, m_innerExceptions[i].ToString(), "<---", Environment.NewLine);
285  }
286  return text;
287  }
288  }
289 }
static CultureInfo InvariantCulture
Gets the T:System.Globalization.CultureInfo object that is culture-independent (invariant).
Definition: CultureInfo.cs:263
static string NewLine
Gets the newline string defined for this environment.
Definition: Environment.cs:449
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
AggregateException(params Exception[] innerExceptions)
Initializes a new instance of the T:System.AggregateException class with references to the inner exce...
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
Provides the base class for a generic read-only collection.
void Handle(Func< Exception, bool > predicate)
Invokes a handler on each T:System.Exception contained by this T:System.AggregateException.
Definition: __Canon.cs:3
int Count
Gets the number of elements contained in the T:System.Collections.ObjectModel.ReadOnlyCollection`1 in...
Exposes the enumerator, which supports a simple iteration over a collection of a specified type....
Definition: IEnumerable.cs:9
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Represents one or more errors that occur during application execution.
override Exception GetBaseException()
Returns the T:System.AggregateException that is the root cause of this exception.
Represents an exception whose state is captured at a certain point in code.
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Definition: List.cs:510
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
AggregateException(string message, params Exception[] innerExceptions)
Initializes a new instance of the T:System.AggregateException class with a specified error message an...
override void GetObjectData(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.AggregateException class with serialized data.
Exception SourceException
Gets the exception that is represented by the current instance.
Exception InnerException
Gets the T:System.Exception instance that caused the current exception.
Definition: Exception.cs:139
The exception thrown when an error occurs during serialization or deserialization.
virtual string Message
Gets a message that describes the current exception.
Definition: Exception.cs:95
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
AggregateException(string message, Exception innerException)
Initializes a new instance of the T:System.AggregateException class with a specified error message an...
Represents a collection of objects that can be individually accessed by index.
Definition: IList.cs:9
Exception()
Initializes a new instance of the T:System.Exception class.
Definition: Exception.cs:286
override string ToString()
Creates and returns a string representation of the current T:System.AggregateException.
int Count
Gets the number of elements contained in the T:System.Collections.Generic.ICollection`1.
Definition: ICollection.cs:15
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
void CopyTo(T[] array, int index)
Copies the entire T:System.Collections.ObjectModel.ReadOnlyCollection`1 to a compatible one-dimension...
ReadOnlyCollection< Exception > InnerExceptions
Gets a read-only collection of the T:System.Exception instances that caused the current exception.
Specifies that the class can be serialized.
AggregateException Flatten()
Flattens an T:System.AggregateException instances into a single, new instance.
AggregateException(string message)
Initializes a new instance of the T:System.AggregateException class with a specified message that des...
AggregateException(IEnumerable< Exception > innerExceptions)
Initializes a new instance of the T:System.AggregateException class with references to the inner exce...
AggregateException(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.AggregateException class with serialized data.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
AggregateException()
Initializes a new instance of the T:System.AggregateException class with a system-supplied message th...