且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当前上下文中不存在名称“__o"

更新时间:2023-02-16 15:25:42

我发现如果我选择 Build Only 而不是 Build + IntelliSense 错误(即与 IntelliSense 相关)将消失.

I found out that if I choose Build Only instead of Build + IntelliSense the errors (that are related to IntelliSense) will go away.

更新 1:原因

发生这种情况的原因是对于这样的代码:

The reason that this is happening is that for codes like this:

<% if (true) { %>
    <%=1%>
<% } %>
<%=2%>

为了在设计时在 块中提供智能感知,ASP.NET 生成对临时 __o 变量和语言(VB 或 C#)的赋值,然后为变量提供智能感知.这是在页面编译器看到第一个 <%= ... %> 块时完成的.但是在这里,块在 if 内,所以在 if 关闭后,变量超出范围.我们最终生成了这样的东西:

In order to provide IntelliSense in <%= %> blocks at design time, ASP.NET generates assignment to a temporary __o variable and language (VB or C#) then provide the IntelliSense for the variable. That is done when page compiler sees the first <%= ... %> block. But here, the block is inside the if, so after the if closes, the variable goes out of scope. We end up generating something like this:

if (true) { 
    object @__o;
    @__o = 1;
}
@__o = 2;

解决方法是在页面的早期添加一个虚拟表达式.例如

The workaround is to add a dummy expression early in the page. E.g.

<%=""%>

这不会渲染任何东西,它会确保在任何潜在的 if(或其他范围)语句之前,__o 在 Render 方法中被声明为***.

This will not render anything, and it will make sure that __o is declared top level in the Render method, before any potential if (or other scoping) statement.

更新 2:在不丢失其他 IntelliSense 错误的情况下摆脱此错误

点击错误列表面板左上角的过滤器按钮,取消勾选CS0103,错误代码为:名称'__o'在当前不存在上下文,这些错误将不再显示,您仍然可以有其他 IntelliSense 错误和警告:

Click on the filter button on the top left corner of the error list panel and uncheck the CS0103 which the error code for the: The name '__o' does not exist in the current context and these errors will not be shown anymore and you can still have other IntelliSense errors and warnings: