首页 > 解决方案 > c# 对象在 Visual Studio 的 Locals 中可见,但在代码中不可用

问题描述

我想知道为什么当我在调试模式下查看 Locals 时,我有一个 Object SelectElements,但是我无法在代码中使用它,Visual Studio 没有在下拉列表中显示它,而且它还带来了一条错误消息,即对象或方法不存在......我错过了什么?如果是范围问题,为什么它在调试中可见?

      TSqlParser parser = new TSql120Parser(true);
        IList<ParseError> parseErrors;
        TSqlFragment sqlFragment = parser.Parse(new StringReader(sql), out parseErrors);

        if (parseErrors.Count > 0) Console.WriteLine("Errors:");
        //parseErrors.Select(e => e.Message.Indent(2)).ToList().ForEach(Console.WriteLine);

        OwnVisitor visitor = new OwnVisitor();
        sqlFragment.Accept(visitor);

        Console.WriteLine("Done.");
        Console.ReadKey();
    }
}

class OwnVisitor : TSqlFragmentVisitor
{
    public override void ExplicitVisit(SelectStatement node)
    {
        QuerySpecification querySpecification = node.QueryExpression as QuerySpecification;

        FromClause fromClause = querySpecification.FromClause;
        NamedTableReference namedTableReference = fromClause.TableReferences[0] as NamedTableReference;
        TableReferenceWithAlias tableReferenceWithAlias = fromClause.TableReferences[0] as TableReferenceWithAlias;

        foreach (var with in node.WithCtesAndXmlNamespaces.CommonTableExpressions)
        {
            var = with.QueryExpression.s

            //QuerySpecification wQs = with.QueryExpression;
        }

视觉工作室

标签: c#visual-studio

解决方案


locals 窗口显示具有所有字段/属性的对象的真实形状(即使它是私有的或未通过接口公开),而 Visual Studio 仅显示可用成员(当前接口允许)。例如:

interface IFoo { }
class Bar : IFoo
{
    public string Prop { get; set; }
}
void Method()
{
    IFoo foo = new Bar();
    foo.Prop; // Here, you will see error from VS, but still can see/update it in Locals
}

因此,在您的情况下,只需检查 Locals 窗口(第三列)中的变量类型和代码中的实际类型,可能它们是不同的。


推荐阅读