@@ -29,9 +29,7 @@ public static void DisposeAll([NotNull, ItemNotNull, InstantHandle] this IEnumer
2929 }
3030 catch ( Exception ex )
3131 {
32- if ( exceptions == null )
33- exceptions = new List < Exception > ( ) ;
34-
32+ exceptions ??= new List < Exception > ( ) ;
3533 exceptions . Add ( ex ) ;
3634 }
3735 }
@@ -74,6 +72,47 @@ public static ValueTask DisposeAsync([NotNull] this IDisposable disposable)
7472 disposable . Dispose ( ) ;
7573 return new ValueTask ( ) ;
7674 }
75+
76+ /// <summary>Invokes the dispose for each item in the <paramref name="disposables"/>.</summary>
77+ /// <param name="disposables">The multiple <see cref="IDisposable"/> instances.</param>
78+ /// <exception cref="AggregateException"></exception>
79+ public static async ValueTask DisposeAllAsync (
80+ [ NotNull , ItemNotNull , InstantHandle ] this IEnumerable < IAsyncDisposable > disposables )
81+ {
82+ List < Exception > exceptions = null ;
83+
84+ foreach ( var item in disposables )
85+ try
86+ {
87+ await item . DisposeAsync ( ) ;
88+ }
89+ catch ( Exception ex )
90+ {
91+ exceptions ??= new List < Exception > ( ) ;
92+ exceptions . Add ( ex ) ;
93+ }
94+
95+ if ( exceptions != null )
96+ throw new AggregateException ( exceptions ) ;
97+ }
98+
99+ /// <summary>Invokes the dispose for each item in the <paramref name="disposables"/>.</summary>
100+ /// <param name="disposables">The multiple <see cref="IDisposable"/> instances.</param>
101+ /// <param name="exceptionHandler">The exception handler.</param>
102+ public static async ValueTask DisposeAllAsync (
103+ [ NotNull , ItemNotNull , InstantHandle ] this IEnumerable < IAsyncDisposable > disposables ,
104+ [ NotNull , InstantHandle ] Func < Exception , bool > exceptionHandler )
105+ {
106+ foreach ( var item in disposables )
107+ try
108+ {
109+ await item . DisposeAsync ( ) ;
110+ }
111+ catch ( Exception ex ) when ( exceptionHandler ( ex ) )
112+ {
113+ ex . LogToCodeTraceSourceOnCatch ( true ) ;
114+ }
115+ }
77116#endif
78117 }
79118}
0 commit comments