实现不带yield 关键字的Linqs Select。无法遵循控制流程
我知道有很多关于 Linq 及其内部工作原理的文章。受到 Jon Skeets EduLinq 的启发,我想揭开 Linq Operators 背后发生的事情的神秘面纱。所以我尝试做的是实现 Linqs Select() 方法,乍一看听起来很无聊。但我实际上想做的是在不使用yield 关键字的情况下实现它。
到目前为止,这就是我所得到的:
class Program
{
static void Main(string[] args)
{
var list = new int[] {1, 2, 3};
var otherList = list.MySelect(x => x.ToString()).MySelect(x => x + "test");
foreach (var item in otherList)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
public static class EnumerableEx
{
public static IEnumerable<R> MySelect<T, R>(this IEnumerable<T> sequence, Func<T, R> apply)
{
return new EnumerableWrapper<R, T>(sequence, apply);
}
}
public class EnumerableWrapper<T, O> : IEnumerable<T>
{
private readonly IEnumerable<O> _sequence;
private readonly Func<O, T> _apply;
public EnumerableWrapper(IEnumerable<O> sequence, Func<O, T> apply)
{
_sequence = sequence;
_apply = apply;
}
public IEnumerator<T> GetEnumerator()
{
return new EnumeratorWrapper<T, O>(_sequence, _apply);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class EnumeratorWrapper<T, O> : IEnumerator<T>
{
private readonly IEnumerator<O> _enumerator;
private readonly Func<O, T> _apply;
public EnumeratorWrapper(IEnumerable<O> sequence, Func<O, T> apply)
{
_enumerator = sequence.GetEnumerator();
_apply = apply;
}
public void Dispose()
{
}
public bool MoveNext()
{
var hasItems = _enumerator.MoveNext();
if (hasItems)
Current = _apply(_enumerator.Current);
return hasItems;
}
public void Reset()
{
_enumerator.Reset();
}
public T Current { get; private set; }
object IEnumerator.Current
{
get { return Current; }
}
}
它似乎有效。但是,我很难遵循它的控制流程。正如你所看到的,我依赖于预测。这会导致 MoveNext() 方法中发生奇怪的事情(对我来说很奇怪!)。如果在 MoveNext() 方法的每一行中设置断点,您将看到控制流实际上在不同实例之间跳转,并且永远不会在一批中执行该方法。它的跳跃就好像它使用了不同的线程或者我们使用了yield。但归根结底,这只是一个正常的方法,所以我想知道这是怎么回事?
I know a lot has been written about Linq and it's inner workings. Inspired by Jon Skeets EduLinq, I wanted to to demystify what's going on behind the Linq Operators. So what I tried to do is to implement Linqs Select() method which sounds pretty boring at first sight. But what I'm actually trying to do is to implement it without the use of the yield keyword.
So here is what I got so far:
class Program
{
static void Main(string[] args)
{
var list = new int[] {1, 2, 3};
var otherList = list.MySelect(x => x.ToString()).MySelect(x => x + "test");
foreach (var item in otherList)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
public static class EnumerableEx
{
public static IEnumerable<R> MySelect<T, R>(this IEnumerable<T> sequence, Func<T, R> apply)
{
return new EnumerableWrapper<R, T>(sequence, apply);
}
}
public class EnumerableWrapper<T, O> : IEnumerable<T>
{
private readonly IEnumerable<O> _sequence;
private readonly Func<O, T> _apply;
public EnumerableWrapper(IEnumerable<O> sequence, Func<O, T> apply)
{
_sequence = sequence;
_apply = apply;
}
public IEnumerator<T> GetEnumerator()
{
return new EnumeratorWrapper<T, O>(_sequence, _apply);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public class EnumeratorWrapper<T, O> : IEnumerator<T>
{
private readonly IEnumerator<O> _enumerator;
private readonly Func<O, T> _apply;
public EnumeratorWrapper(IEnumerable<O> sequence, Func<O, T> apply)
{
_enumerator = sequence.GetEnumerator();
_apply = apply;
}
public void Dispose()
{
}
public bool MoveNext()
{
var hasItems = _enumerator.MoveNext();
if (hasItems)
Current = _apply(_enumerator.Current);
return hasItems;
}
public void Reset()
{
_enumerator.Reset();
}
public T Current { get; private set; }
object IEnumerator.Current
{
get { return Current; }
}
}
It seems to work. However, I have trouble to follow it's control flow. As you can see, I chain to projections. This causes to let strange things (strange for me!) happen in the MoveNext() method. If you set breakpoints in every line of the MoveNext() method you will see that the control flow actually jumps between the different instances and never works through the method in one batch. It's jumping as if it were using different threads or if we were using yield. But in the end, it's just a normal method so I wonder what's going on there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
每次您对结果调用
MoveNext()
时,它都会:MoveNext()
,这将MoveNext()
,这将...MoveNext()
(数组的),它将返回一个数字。MoveNext()
将调用投影x =>; x.ToString()
以便它Current
成员MoveNext()
然后将调用投影x =>; x + "test"
对X.Current
的结果进行测试,并将结果存储在Y.Current
中所以这里没有什么特别神奇的事情 - 你'我们刚刚接到了一堆电话,就像平常一样。调试体验将向您展示从一个
EnumeratorWrapper.MoveNext
到另一个的调用;唯一奇怪的跳跃是当您单步执行在 Main 方法中声明的投影时。如果这不能解释是什么让您感到困惑,请详细说明您不理解流程的具体情况,我会尽力提供帮助。 (很高兴看到你想更多地了解所有这些事情是如何运作的。很高兴看到志同道合的人!)
Each time you call
MoveNext()
on the result it will:MoveNext()
on the final iterator (which I'll call Y), which will...MoveNext()
on the previous iterator (which I'll call X), which will...MoveNext()
on the original iterator (of the array), which will return a number.MoveNext()
in X will then call the projectionx => x.ToString()
so that it has an appropriateCurrent
memberMoveNext()
in Y will then call the projectionx => x + "test"
on the result ofX.Current
, and store the result inY.Current
So there's nothing particularly magical going on here - you've just got stacked calls, just as normal. The debugging experience will show you that call from one
EnumeratorWrapper.MoveNext
to another; the only odd jumping around will be when you step through the projections, which are declared in theMain
method.If that doesn't explain what was confusing you, please give more details about exactly where you don't understand the flow, and I'll see how I can help. (It's great to see you wanting to learn more about how all these things work though. Good to see a kindred spirit!)