forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisposableExtensions.cs
More file actions
46 lines (42 loc) · 1.25 KB
/
DisposableExtensions.cs
File metadata and controls
46 lines (42 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Collections.Generic;
using ServiceStack.Logging;
namespace ServiceStack.Common
{
public static class DisposableExtensions
{
public static void Dispose(this IEnumerable<IDisposable> resources, ILog log)
{
foreach (var disposable in resources)
{
try
{
disposable.Dispose();
}
catch (Exception ex)
{
if (log != null)
{
log.Error(string.Format("Error disposing of '{0}'", disposable.GetType().FullName), ex);
}
}
}
}
public static void Dispose(this IEnumerable<IDisposable> resources)
{
Dispose(resources, null);
}
public static void Dispose(params IDisposable[] disposables)
{
Dispose(disposables, null);
}
public static void Run<T>(this T disposable, Action<T> runActionThenDispose)
where T : IDisposable
{
using (disposable)
{
runActionThenDispose(disposable);
}
}
}
}