调试时无法进入迭代器块(C#) [英] Can't step into iterator block whilst debugging (C#)
问题描述
我正在尝试从单元测试项目中调试我的代码,但是当我尝试进入一个方法时,它直接传递到下一行,并且该方法中的断点不被打。该方法是在一个不同的项目中的类,但是所有的代码都是以调试模式构建的,我已经尝试清理并重新构建解决方案,而不用快乐。
I'm trying to debug my code which is being executed from a unit test project, but when I try to step into a method, it just passes straight onto the next line and the breakpoint inside that method isn't hit. The method is on a class which is in a different project, but all the code is built in debug mode and I've tried cleaning and rebuilding the solution with no joy.
但是,这仅仅是因为我在方法中添加了一个迭代器块。当我删除它并重建,我可以罚款。奇怪?
However, this has only happened since I added an iterator block to the method. When I remove it and rebuild, I can step in fine. Weird?
我正在使用Visual Studio 2010 Beta 1,这可能只是一个错误吗?
I am using Visual Studio 2010 Beta 1, could this just be a bug?
推荐答案
迭代器块使用延迟执行 - 意思是:直到你真正开始迭代数据,没有任何执行。
Iterator blocks use deferred execution - meaning: until you actually start iterating over the data, nothing is executed.
所以:迭代?是否循环了价值观?如果您需要尽可能早地添加验证逻辑,那么您目前需要两种方法:
So: has the data been iterated? Is anything looping over the values? If you need to add validation logic that runs as early as possible, you currently need two methods:
public static IEnumerable<int> GetNumbers(int from, int to) {
// this validation runs ASAP (not deferred)
if (to < from) throw new ArgumentOutOfRangeException("to");
return GetNumbersCore(from, to);
}
private static IEnumerable<int> GetNumbersCore(int from, int to) {
// this is all deferred
while (from <= to) {
yield return from++;
}
}
这篇关于调试时无法进入迭代器块(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!