forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSafetyEnumerable.cs
46 lines (38 loc) · 1.04 KB
/
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
41
42
43
44
45
46
using System.Collections;
using System.Collections.Generic;
namespace NHibernate.Util
{
/// <summary>
/// Used to ensecure 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)
{
this.collection = collection;
}
#region IEnumerable<T> Members
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
IEnumerator enumerator = collection.GetEnumerator();
while(enumerator.MoveNext())
{
object element = enumerator.Current;
if(typeof(T).IsAssignableFrom(element.GetType()))
yield return (T)element;
}
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
#endregion
}
}