diff --git a/src/libraries/System.Private.CoreLib/src/System/AggregateException.cs b/src/libraries/System.Private.CoreLib/src/System/AggregateException.cs index 46baebfddd6e7b..204a4f06ca9aad 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AggregateException.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AggregateException.cs @@ -377,7 +377,7 @@ public AggregateException Flatten() while (exceptionsToFlatten.Count > nDequeueIndex) { // dequeue one from exceptionsToFlatten - IList currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions; + ReadOnlyCollection currentInnerExceptions = exceptionsToFlatten[nDequeueIndex++].InnerExceptions; for (int i = 0; i < currentInnerExceptions.Count; i++) { @@ -401,8 +401,7 @@ public AggregateException Flatten() } } - - return new AggregateException(Message, flattenedExceptions); + return new AggregateException(GetType() == typeof(AggregateException) ? base.Message : Message, flattenedExceptions); } /// Gets a message that describes the exception. diff --git a/src/libraries/System.Threading.Tasks/tests/AggregateExceptionTests.cs b/src/libraries/System.Threading.Tasks/tests/AggregateExceptionTests.cs index fdf4a5dac29a36..5692231026432e 100644 --- a/src/libraries/System.Threading.Tasks/tests/AggregateExceptionTests.cs +++ b/src/libraries/System.Threading.Tasks/tests/AggregateExceptionTests.cs @@ -2,10 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using Xunit; -using System; using System.Collections.Generic; -using System.Diagnostics; +using Xunit; namespace System.Threading.Tasks.Tests { @@ -103,7 +101,9 @@ public static void Flatten() Exception exceptionB = new Exception("B"); Exception exceptionC = new Exception("C"); - AggregateException aggExceptionBase = new AggregateException(exceptionA, exceptionB, exceptionC); + AggregateException aggExceptionBase = new AggregateException("message", exceptionA, exceptionB, exceptionC); + + Assert.Equal("message (A) (B) (C)", aggExceptionBase.Message); // Verify flattening one with another. // > Flattening (no recursion)... @@ -114,15 +114,17 @@ public static void Flatten() }; Assert.Equal(expected1, flattened1.InnerExceptions); + Assert.Equal("message (A) (B) (C)", flattened1.Message); // Verify flattening one with another, accounting for recursion. - AggregateException aggExceptionRecurse = new AggregateException(aggExceptionBase, aggExceptionBase); + AggregateException aggExceptionRecurse = new AggregateException("message", aggExceptionBase, aggExceptionBase); AggregateException flattened2 = aggExceptionRecurse.Flatten(); Exception[] expected2 = new Exception[] { exceptionA, exceptionB, exceptionC, exceptionA, exceptionB, exceptionC, }; Assert.Equal(expected2, flattened2.InnerExceptions); + Assert.Equal("message (A) (B) (C) (A) (B) (C)", flattened2.Message); } } }