I was about to build my own IEnumerable class that performs some action on all items the first time something iterates over it then I started wondering, does the framework already have something that I could use?
Here's what I was building so you have an idea what I'm looking for:
public class DelayedExecutionIEnumerable<T> : IEnumerable<T>
{
IEnumerable<T> Items;
Action<T> Action;
bool ActionPerformed;
public DelayedExecutionIEnumerable(IEnumerable<T> items, Action<T> action)
{
this.Items = items;
this.Action = action;
}
void DoAction()
{
if (!ActionPerformed)
{
foreach (var i in Items)
{
Action(i);
}
ActionPerformed = true;
}
}
#region IEnumerable<IEntity> Members
public IEnumerator<T> GetEnumerator()
{
DoAction();
return Items.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
DoAction();
return Items.GetEnumerator();
}
#endregion
}