如何组合DebuggerHidden与迭代器块方法? [英] How to combine DebuggerHidden with iterator block methods?

查看:237
本文介绍了如何组合DebuggerHidden与迭代器块方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DebuggerHidden 对于标记助手方法非常方便,确保未处理的异常在方便的地方停止调试器:

DebuggerHidden is pretty handy for marking helper methods, making sure unhandled exceptions stop the debugger in a convenient place:

                 

                 

不幸的是,这不似乎可以使用迭代器块:

Unfortunately this doesn't seem to work with iterator blocks:

       

         

(如果有的话,调试器将在中显示作为第二个示例中的当前语句)。

(if it did, the debugger would show the in as the current statement in the second example).

虽然这显然是一个限制的Visual Studio(为此我已经提交报告),还有一些方法我可以解决这个问题,同时仍然使用迭代器块?

While this is obviously a limitation of Visual Studio (for which I've submitted a report), is there some way I could perhaps work around this issue while still using an iterator block?

我猜这是因为编译器生成的代码来实现迭代器没有标记为 [DebuggerHidden] 。也许有一些方法来说服编译器这样做?

I am guessing that this happens because the compiler-generated code to implement the iterator isn't marked with [DebuggerHidden]. Perhaps there's some way to convince the compiler to do that?

推荐答案

也许不是你希望的答案,但作为一种解决方法获得一些积分...不知道如果你已经梦想了这个。

Maybe not the answer you hoped for but as a workaround it could gain some points...not sure if you already dreamed this up.

有一个帮助类可以展开迭代器,然后使用扩展方法来使包装器发挥作用在你的迭代器上
我处理异常并重新抛出。在VS2010中,我不得不奇怪地取消选中启用我的代码的调试选项,以获得接近OP要求的行为。离开选项的选项仍然会让你在实际的迭代器中,但是ik看起来像一行太远。

Have a helper class that unwraps your iterator and then use an extension method to bring the wrapper into play on your iterator. I handle exceptions and rethrow. In VS2010 I had to strangely Uncheck the debug option 'Enable just my code' to get behavior close to what OP asked for. Leaving the option checked still drops you in the actual iterator but ik looks like one line too far.

这使得这个答案更多的是一个实验来证明和支持更好的编译器支持需要这个方案工作。

That makes this answer more an experiment to prove and underpin that better compiler support is needed for the scenario to work.

扩展方法助手类:

public static class HiddenHelper
{
    public static HiddenEnumerator<T> Hide<T>(this IEnumerable<T> enu )
    {
        return HiddenEnumerator<T>.Enumerable(enu);
    }
}

包装:

public class HiddenEnumerator<T> : IEnumerable<T>, IEnumerator<T>
    {
        IEnumerator<T> _actual;
        private HiddenEnumerator(IEnumerable<T> enu)
        {
            _actual = enu.GetEnumerator();
        }

        public static HiddenEnumerator<T> Enumerable(IEnumerable<T> enu )
        {
            return new HiddenEnumerator<T>(enu);
        }

        public T Current
        {
            [DebuggerHidden]
            get 
            { 
                T someThing = default(T);
                try
                {
                   someThing = _actual.Current;
                }
                catch
                {
                   throw new Exception();
                }
                return someThing; 
            }
        }

        public IEnumerator<T> GetEnumerator()
        {
            return this;
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            _actual.Dispose();
        }

        object IEnumerator.Current
        {
            get { return _actual.Current; }
        }

        [DebuggerHidden]
        public bool MoveNext()
        {
                bool move = false;
                try
                {
                    move = _actual.MoveNext();
                }
                catch
                {
                     throw new IndexOutOfRangeException();
                }
               return move;
        }

        public void Reset()
        {
            _actual.Reset();
        }
   }

用法:

        public IEnumerable<int> Power(int number, int exponent)
        {
            int counter = 0;
            int result = 1;
            while (counter++ < exponent)
            {
                if (result>Int16.MaxValue) throw new Exception();
                result = result * number;
                yield return result;
            }
        }

        public void UseIt()
        {
            foreach(var i in Power(Int32.MaxValue-1,5).Hide())
            {
                Debug.WriteLine(i);
            }
        }

这篇关于如何组合DebuggerHidden与迭代器块方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆