forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafetyEnumerable.cs
40 lines (36 loc) · 920 Bytes
/
SafetyEnumerable.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
using System.Collections;
using System.Collections.Generic;
namespace NHibernate.Util
{
/// <summary>
/// Used to ensure a collection filtering a given IEnumerable by a certain type.
/// </summary>
/// <typeparam name="T">The type used like filter.</typeparam>
public class SafetyEnumerable<T> : IEnumerable<T>
{
/*
* This class was created to filter List<ISelectable> to an IEnumerable<Column>
*/
private readonly IEnumerable _collection;
public SafetyEnumerable(IEnumerable collection)
{
_collection = collection;
}
public IEnumerator<T> GetEnumerator()
{
var enumerator = _collection.GetEnumerator();
while (enumerator.MoveNext())
{
var element = enumerator.Current;
if (element == null)
yield return default(T);
else if (element is T)
yield return (T) element;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}