-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Using.cs
58 lines (55 loc) · 2.1 KB
/
Using.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Generates a sequence that's dependent on a resource object whose lifetime is determined by the sequence
/// usage duration.
/// </summary>
/// <typeparam name="TSource">
/// Source element type.
/// </typeparam>
/// <typeparam name="TResource">
/// Resource type.
/// </typeparam>
/// <param name="resourceFactory">
/// Resource factory function.
/// </param>
/// <param name="enumerableFactory">
/// Enumerable factory function, having access to the obtained resource.
/// </param>
/// <returns>
/// Sequence whose use controls the lifetime of the associated obtained resource.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="resourceFactory"/> or <paramref name="enumerableFactory"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
/// <paramref name="resourceFactory"/> and <paramref name="enumerableFactory"/> are evaluated lazily, once
/// enumeration has begun. The value returned by <paramref name="resourceFactory"/> will be disposed after the
/// enumeration has completed.
/// </para>
/// <para>
/// The values returned by <paramref name="enumerableFactory"/> and <paramref name="enumerableFactory"/> are not
/// cached; multiple iterations of the <see cref="IEnumerable{T}"/> returned by this method will call these
/// methods separately for each iteration.
/// </para>
/// </remarks>
public static IEnumerable<TSource> Using<TSource, TResource>(
Func<TResource> resourceFactory,
Func<TResource, IEnumerable<TSource>> enumerableFactory)
where TResource : IDisposable
{
ArgumentNullException.ThrowIfNull(resourceFactory);
ArgumentNullException.ThrowIfNull(enumerableFactory);
return Core(resourceFactory, enumerableFactory);
static IEnumerable<TSource> Core(
Func<TResource> resourceFactory,
Func<TResource, IEnumerable<TSource>> enumerableFactory)
{
using var resource = resourceFactory();
foreach (var item in enumerableFactory(resource))
yield return item;
}
}
}