c# - 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;
}
解决方案
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 窗口(第三列)中的变量类型和代码中的实际类型,可能它们是不同的。
推荐阅读
- twitter-bootstrap - 覆盖 Bootstrap 的单选按钮
- javascript - 为什么这个窗口在关闭后没有打开(chrome 扩展)?
- javascript - 如何在存储过程中将容器名称作为 Azure cosmos DB 查询的参数传递?
- python-3.x - 如何使用 Python 中的 Plotly Express 在同一 y 轴上绘制多条线并放置 y 标签、垂直线
- html - Mathjax3 溢出引导列
- swift - 如何在另一个类中使用 addTarget 定位选择器
- java - 执行单元测试时spring boot应用程序是否需要连接到数据库
- reactjs - React Native 无法从不同组件的函数体内更新组件
- ms-access - 将 4 行合并为 1 行,所有行都具有唯一值
- r - 为每个类别分配颜色并在 R 中的 Leaflet Map 中显示点