我正要构建我自己的IEnumerable类,它会在第一次迭代它时对所有项执行某些操作然后我开始疑惑,框架是否已经有了我可以使用的东西?
这就是我正在构建的内容,因此您可以了解我正在寻找的内容:
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
}
答案 0 :(得分:2)
我不确定有什么东西能完全符合您的要求,但我建议您使用Lazy&lt; T&gt;为此,它将处理线程安全问题(针对每个单独的项目):
public class DelayedExecutionIEnumerable<T> : IEnumerable<T>
{
List<Lazy<T>> LazyItems;
public DelayedExecutionIEnumerable(IEnumerable<T> items, Action<T> action)
{
// Wrap items into our List of Lazy items, the action predicate
// will be executed only once on each item the first time it is iterated.
this.LazyItems = items.Select(
item => new Lazy<T>(
() =>
{
action(item);
return item;
},
true)).ToList(); // isThreadSafe = true
}
#region IEnumerable<IEntity> Members
public IEnumerator<T> GetEnumerator()
{
return this.LazyItems.Select(i => i.Value).GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.LazyItems.Select(i => i.Value).GetEnumerator();
}
#endregion
}
答案 1 :(得分:2)