【问题标题】:"using static" directive doesn't cooperate with Watch window“使用静态”指令不与监视窗口合作
【发布时间】:2018-03-28 21:10:52
【问题描述】:

我正在使用来自C#6using static 指令来引用枚举的值而不提供其类型名,这对可读性很有帮助。不幸的是,当我在调试期间尝试向 Watch 窗口添加这样的引用时,它给了我一个 CS0103 错误。这是一个例子:

using System.Collections.Generic;
using static ConsoleApplication1.MyEnumType;

namespace ConsoleApplication1
{
    public enum MyEnumType
    {
        thing1,
        thing2,
    }
    class Program
    {
        static void Main()
        {
            Dictionary<MyEnumType, int> dict = new Dictionary<MyEnumType, int>();

            // add the left-hand side of these expressions to watch window:
            dict[MyEnumType.thing1] = 1;  //Watch Value = 1
            dict[thing2] = 2;             //Watch Value = error CS0103: The name 'thing2' does not exist in the current context
        }
    }
}

由于 using static 指令,最后一行代码编译得很好。但是在调试时,如果我尝试将 dict[thing2] 添加到 Watch 窗口,我会收到错误消息。有没有办法解决这个问题?


更新:这是has been reported 的一个已知错误,但截至今天(我提交错误报告 9 个月后),还没有尝试解决它。当然,解决方法是简单地将类型名称添加到监视窗口(虽然很明显,但当我第一次发布此内容时,我却忽略了)。

【问题讨论】:

标签: c# debugging visual-studio-2017 c#-6.0 using-directives


【解决方案1】:

我通过 Reflector 运行了您的示例应用程序,这是生成的 Main 方法:

Dictionary<MyEnumType, int> dictionary = new Dictionary<MyEnumType, int>();
dictionary.set_Item(MyEnumType.thing1, 1);
dictionary.set_Item(MyEnumType.thing2, 2);

编译器将MyEnumType 添加到thing2 的开头。由于它似乎是编译器功能,因此您无法在运行时使用快捷方式(在 Watch 或 Immediate 窗口中)。

【讨论】:

  • 假设调试器(仅)查看文字源代码。我不知道,但它也有 Reflection 和 PDB 文件可供依赖。
  • 实际上,它假定调试器没有使用文字源代码。相反,它正在处理已编译的二进制文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多