奇怪:找不到 C# 类型或命名空间名称 - 构建成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15395042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:39:22  来源:igfitidea点击:

Weird: C# Type or Namespace name could not be found - Builds successfully

c#.netvisual-studiovisual-studio-2012project-reference

提问by hofnarwillie

I have a weird error showing up in my project when it is open in the VS2012 IDE. Everywhere where I make use of another referenced project it suddenly says "Type or Namespace name could not be found". And by "says", I mean it has the text underlined in red with the error when I hover over it. The intellisense doesn't work for that code. BUT (and here's the weird part), the errors do not show up in the error console and the project builds and runs fine.

当它在 VS2012 IDE 中打开时,我的项目中出现一个奇怪的错误。在我使用另一个引用项目的任何地方,它突然说"Type or Namespace name could not be found"。通过“说”,我的意思是当我将鼠标悬停在它上面时,它的文本带有红色下划线和错误。智能感知不适用于该代码。但是(这是奇怪的部分),错误不会显示在错误控制台中,并且项目构建并运行良好。

I can even debug and step through the code and it works perfectly fine. So at runtime the project is referenced fine but at design time the IDE can't find it. This worked for the past 2 weeks, and only then suddenly went a little bonkers. It's really annoying because I am rubbish at coding without intellisense!

我什至可以调试和单步调试代码,它运行得非常好。所以在运行时,项目被很好地引用,但在设计时 IDE 找不到它。这在过去的 2 周内有效,然后才突然变得有点疯狂。这真的很烦人,因为我在没有智能感知的情况下编码很垃圾!

Has anybody ever seen anything like this or have any suggestions?

有没有人见过这样的事情或有什么建议?

采纳答案by Lainezor

I have found this is a known problem with VS2012. Check to see how you are building, 64 bit or 32 bit. It won't work with 64 bit but it will with 32 bit. It will say things are missing and design will not work, however the program will run fine. I have heard the new VS update that hasn't been released yet will fix it.

我发现这是 VS2012 的一个已知问题。检查您的构建方式,64 位或 32 位。它不适用于 64 位,但适用于 32 位。它会说缺少东西并且设计将无法工作,但是程序会运行良好。我听说尚未发布的新 VS 更新将修复它。

回答by Parimal Raj

Make sure that there is no classwith the name same as Project default namespace.

确保没有class与 Project 同名的项目default namespace

回答by Maor Hayoun

make sure that you don't reference .net framework 4.5 projects in .net 4 projects!

确保您没有在 .net 4 项目中引用 .net framework 4.5 项目!

回答by Yevgen Fesenko

Manually delete all the references to the libraries of the other projects and re-add. Intellisense rebuilds whatever it needs at that point and doing this has helped me in the past.

手动删除对其他项目库的所有引用并重新添加。Intellisense 会重建当时它需要的任何东西,过去这样做对我有帮助。

回答by txavier

I had this. I referenced assemblies whose "Target Framework" were set to ".Net Framework 4" in the "Application" secion of the projects properties. I changed this to ".Net Framework 4.5" not just in the referenced assembly's project but also the project I was building and it worked. Give this a try.

我有这个。我在项目属性的“应用程序”部分引用了“目标框架”设置为“.Net Framework 4”的程序集。我不仅在引用的程序集项目中将其更改为“.Net Framework 4.5”,而且还在我正在构建的项目中将其更改为“.Net Framework 4.5”并且它有效。试试这个。

回答by Dirk Brockhaus

Another source of this problem is a solution with multiple projects containing code for the same namespace. The compiler can handle this. Intellisense won't.

此问题的另一个来源是多个项目包含同一命名空间的代码的解决方案。编译器可以处理这个。智能感知不会。

回答by classicskids

Examine your .proj files in a text editor and make sure the paths to your references are correct.

在文本编辑器中检查您的 .proj 文件,并确保您的引用的路径正确。

回答by Greg

Related to a couple other answers here, I had a "Data" project using a "Data" namespace. Built fine, but just started recently showing errors from intellisense (even though it continued building fine.) (VS 2015.)

与此处的其他几个答案相关,我有一个使用“Data”命名空间的“Data”项目。构建良好,但最近才开始显示来自智能感知的错误(即使它继续构建良好。)(VS 2015。)

I fixed this by changing my "Data" project and namespace to "MyCompany.Data".

我通过将我的“Data”项目和命名空间更改为“MyCompany.Data”来解决这个问题。

Oddly, the problem didn't seem to show up until recently, but making the change did fix it. Presumably there was a conflict in namespace with another project or reference, which can build fine, but intellisense can't handle.

奇怪的是,这个问题似乎直到最近才出现,但进行更改确实解决了它。推测命名空间与另一个项目或引用存在冲突,这可以很好地构建,但智能感知无法处理。

You can change this in the Application tab of Project properties. You can also open up an EDMX diagram, right click, choose model browser, choose the second collapsable item in the model browser tab, hit properties, and there you'll find the Namespace optionfor generated Entity Framework entities and contexts. (Similiarly, if you modify an Entity Framework Model's namespace, you might also need to change the related connection to match [in Web.Config for ASP.Net and MVC.])

您可以在项目属性应用程序选项卡中更改此设置。您还可以打开一个EDMX 图,右键单击,选择模型浏览器,在模型浏览器选项卡中选择第二个可折叠项,点击属性,然后您将在那里找到用于生成的实体框架实体和上下文的命名空间选项。(同样,如果您修改实体框架模型的命名空间,您可能还需要更改相关连接以匹配 [在 ASP.Net 和 MVC 的 Web.Config 中。])

回答by drifter

This can also happen if you set the Build Actionto "None" on the referenced file and forget about it.

如果您在引用的文件上将Build Action设置为“None”并忘记它,也会发生这种情况。

回答by EatATaco

I had the same problem, where it would build and run fine, it just would always show that error and I couldn't use intellisense with the class.

我遇到了同样的问题,它可以正常构建和运行,但它总是会显示该错误,并且我无法在类中使用智能感知。

I actually used the automatic method of creating the class in a new file to resolve the issue, then just copied the code over from the real class. I deleted the old file, renamed the new file, and now it works.

我实际上使用了在新文件中创建类的自动方法来解决这个问题,然后只是从真正的类中复制了代码。我删除了旧文件,重命名了新文件,现在它可以工作了。