简体   繁体   English

Visual Studio 说当前上下文中不存在名称

[英]Visual Studio saying name doesn't exist in current context

I am calling a static method on a class like我正在像这样的类上调用静态方法

Foo.bar()

Visual studio's intellisense recognizes Foo and autocompletes bar for me (it highlights Foo and everything like it is working fine). Visual Studio 的智能感知为我识别 Foo 并自动完成栏(它突出显示 Foo 并且一切正常)。 Everything looks fine until I go to build the project, and it throws an error saying the name Foo doesn't exist in current context.在我开始构建项目之前,一切看起来都很好,它抛出一个错误,指出当前上下文中不存在名称 Foo。

I am using this static method call in other files, so I know the class is ok.我在其他文件中使用这个静态方法调用,所以我知道这个类没问题。 The situation is too big to post code, so I am mostly looking for reasons to start looking into that would cause intellisense to function normally but get errors on compile like this.情况太大而无法发布代码,所以我主要是在寻找开始调查的原因,这会导致智能感知正常运行,但在编译时会出现这样的错误。

I've seen this error caused by differing versions of the .NET framework in the different projects.我已经看到这个错误是由不同项目中不同版本的 .NET 框架引起的。 The Class Library I built was 4.5 and the application was 4.0, but the only error it gave was namespace errors.我构建的类库是 4.5,应用程序是 4.0,但它给出的唯一错误是命名空间错误。 Changing the framework version on the class library and rebuilding it, then the application, resolved the error.更改类库上的框架版本并重建它,然后是应用程序,解决了错误。

This can occur when namespaces, classes and variables become tangled when they have the same name.当命名空间、类和变量在名称相同时变得混乱时,就会发生这种情况。 I have suffered with this before.我以前受过这种苦。 Intellisense told me I was right, the compiler told me I was wrong!智能感知告诉我我是对的,编译器告诉我我错了! I trusted the compiler!我相信编译器!

You have 2 options that I can think of你有 2 个我能想到的选择

  1. Search your code for Foo, and see it it is being used for something other than the static class.在你的代码中搜索 Foo,看看它被用于静态类以外的东西。

  2. Fully qualify the Foo.bar() call.完全限定 Foo.bar() 调用。 MyApplication.This.That.Foo.bar();

Do it in that order...it's better to elegantly resolve the issue so you can just call Foo.bar() as this is more readable and maintainable than having MyApplication.This.That.Foo.bar();按这个顺序做……最好优雅地解决问题,这样你就可以调用 Foo.bar() ,因为这比MyApplication.This.That.Foo.bar();更具可读性和可维护性MyApplication.This.That.Foo.bar(); all over the place!到处都是!

在我的例子中,我在代码中间的一个方法的末尾遗漏了一个} ,这导致程序看不到其余的代码并抱怨我在那之后定义的方法。

I know this is a bit old topic, but I just experienced the same and for me it was because the file was not actually included in the solution.我知道这是一个有点老的话题,但我刚刚经历了同样的事情,对我来说这是因为该文件实际上并未包含在解决方案中。

I properly happened because I had renamed the class and then the file, which caused Visual Studio to still know the class and the namespace, but the compiler did not get the file as the renamed file was not included.我正确地发生了,因为我重命名了类,然后重命名了文件,这导致 Visual Studio 仍然知道类和命名空间,但是编译器没有得到文件,因为重命名的文件没有包含在内。

Old thread I know, but I've encountered this issue when referencing a static method from within a unit test project - intellisense said the method was there, but when I tried to build/run the test (in Debug mode) I got the error 'name doesn't exist in current context'.我知道旧线程,但是在从单元测试项目中引用静态方法时遇到了这个问题 - 智能感知说该方法在那里,但是当我尝试构建/运行测试(在调试模式下)时,我得到了错误'名称在当前上下文中不存在'。 In order to fix it I had to rebuild the project containing the referenced static method in Debug configuration (it had only previously been built in Release configuration) - after this the test built and ran OK.为了修复它,我必须在调试配置中重建包含引用的静态方法的项目(它之前仅在发布配置中构建) - 在此之后,测试构建并运行正常。

Consider doing a Clean and then a Build on the project with the problem.考虑对有问题的项目进行清理,然后进行构建。 It is possible for the editor and Intellisense to correctly discover the class, while the compiler works with files that are out-of-date.编辑器和 Intellisense 有可能正确发现类,而编译器则处理过时的文件。 (I had this same problem, and that's how I resolved it.) (我遇到了同样的问题,这就是我解决它的方法。)

this is an old article I know, but I just encountered this issue and has been puzzling me for couple of days, and eventually got to it: click on the class file, in Solution Explorer, then look at the Properties tab;这是我知道的一篇旧文章,但我刚刚遇到这个问题并且让我困惑了几天,最终得到了解决:在解决方案资源管理器中单击类文件,然后查看属性选项卡; make sure Build Action is set to "Compile".确保构建操作设置为“编译”。

Adjust the related file.调整相关文件。 If the error code in Default.aspx.cs , you need to change the top line in the file Default.aspx as below:如果Default.aspx.cs的错误代码,您需要更改文件Default.aspx的第一行,如下所示:

Replace "CodeFile=" with "CodeBehind""CodeFile="替换为"CodeBehind"

Hope this can help.希望这能有所帮助。

-Thanks, Thai_FUV -谢谢, Thai_FUV

I have run into this probelm a few times and so when I do, the first thing I check is if the assembly not recognized has any Nuget packages.我遇到过几次这个问题,所以当我这样做时,我检查的第一件事是无法识别的程序集是否有任何 Nuget 包。 In my cases they always have and I simply forgot to install the same packages in the assembly of which the reference to the un-recognized assembly is in. A re-build command and problem fixed.在我的情况下,他们总是有,我只是忘记在程序集中安装相同的包,其中对无法识别的程序集的引用所在的程序集。重新构建命令并修复了问题。 I hope this helps someone.我希望这可以帮助别人。 This same error message can be given for multiple things so this particular case, may not apply.可以针对多种情况给出相同的错误消息,因此这种特殊情况可能不适用。 If you have not used Nuget than I would suggest trying the other answers如果您没有使用过 Nuget,我建议您尝试其他答案

I also was running into this issue creating a data access layer and had static methods being called with the same symptoms: Intellisense finding it but not the compiler.我也在创建数据访问层时遇到了这个问题,并且调用了具有相同症状的静态方法:Intellisense 找到了它,但没有找到编译器。 I tried many of the above, including fixing the .Net version.我尝试了上面的许多方法,包括修复 .Net 版本。

When adding the source files to the project I also changed the namespace.将源文件添加到项目时,我还更改了命名空间。 With the file with the issue, I forgot to change the namespace to match when it was imported at another time.对于有问题的文件,我忘记更改命名空间以匹配在其他时间导入时的命名空间。

Closing all tabs of MonoDevelop.关闭 MonoDevelop 的所有选项卡。 Then Closing MonoDevelop.然后关闭 MonoDevelop。 Finally opening MonoDevelop again solved the problem for me.最后再次打开 MonoDevelop 为我解决了问题。

Mine was a little more convoluted solution.我的是一个更复杂的解决方案。 Project A referenced projects B and C: both references had Copy Local to true and both produced assemblies with identical names.项目 A 引用了项目 B 和 C:两个引用都将 Copy Local 设为 true,并且都生成了名称相同的程序集。 When building the referencing project, the output assemblies from projects B and C were copied and one overwrote the other because they had the same name.在构建引用项目时,项目 B 和 C 的输出程序集被复制,并且一个覆盖另一个,因为它们具有相同的名称。 VS was then looking for the references within the build directory and only found the assembly that had "won." VS 然后在构建目录中查找引用,并且只找到“获胜”的程序集。

My solution to this problem that occurs every now and then:我对这个时不时发生的问题的解决方案:

  1. Find the class that is giving you problems in the Solution Explorer and "Exclude From Project"在解决方案资源管理器和“从项目中排除”中找到给您带来问题的类
  2. Rebuild that assembly (let's call it "A")重建该程序集(我们称之为“A”)
  3. The project that used the file ("B") will ask you to "Reload" project, wait使用文件(“B”)的项目会要求你“重新加载”项目,等待
  4. Add the file back into assembly A, that you just removed it from, and rebuild将文件重新添加到您刚刚从中删除的程序集 A 中,然后重新构建
  5. Now, reload project B现在,重新加载项目 B

Then the file was found in VS and all was well.然后在VS中找到该文件,一切正常。

In my case I had to reload the project that was marked "missing".就我而言,我不得不重新加载标记为“丢失”的项目。

  1. Project > Unload Project项目 > 卸载项目
  2. Project > Load Project项目 > 加载项目
  3. Clean, Build Solution清洁,构建解决方案

Changing the id of the control resolved the issue for me.更改控件的 ID 为我解决了这个问题。 Apparently the id of the control existed in another part of the solution.显然控件的 id 存在于解决方案的另一部分。

In my case, I was missing the following lines in my csproj file就我而言,我的 csproj 文件中缺少以下几行

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE</DefineConstants>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

Once I added this, I could see the variables while debugging一旦我添加了这个,我就可以在调试时看到变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM