简体   繁体   English

C# 未定义或导入预定义类型“System.Object”

[英]C# Predefined type 'System.Object' is not defined or imported

In .NET core project, I changed the all projects name than after reloading all project that time many errors were showing approx 3225 ..NET 核心项目中,我更改了所有项目的名称,而不是重新加载所有项目后,当时许多错误显示大约3225 And mainly all errors are related to system namespace like主要是所有错误都与系统命名空间有关

  • System.Object not defined or imported System.Object未定义或导入
  • System.Boolean not defined or imported System.Boolean未定义或导入
  • Task does not exist Task不存在
  • namespace ArgumentNullException could not be found找不到命名空间ArgumentNullException

How do I resolve all those errors?如何解决所有这些错误?

  • close VS关闭VS
  • delete the bin , obj and .vs (may be hidden) folders删除binobj.vs (可能是隐藏的)文件夹
  • manually run dotnet restore手动运行dotnet restore
  • reopen the project in VS在 VS 中重新打开项目

That fixes most things.这可以解决大多数问题。

I was hitting this in VS Code.我在 VS Code 中遇到了这个问题。 In Windows Terminal, dotnet build and dotnet run ran fine, but I was getting the errors in VS Code.在 Windows 终端中, dotnet builddotnet run运行良好,但我在 VS Code 中遇到了错误。 The delete/restore/reopen answer did not fix it.删除/恢复/重新打开答案没有解决它。

It seems it was an issue where I had an outdated version of Visual Studio confusing the C#/OmniSharp extension in VS Code.似乎这是一个问题,我有一个过时的 Visual Studio 版本混淆了 VS Code 中的 C#/OmniSharp 扩展。

The OmniSharp log included something like this: OmniSharp 日志包括如下内容:

[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 2 MSBuild instance(s)
            1: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
            2: StandAlone 17.0.0 - "c:\Users\foo\.vscode-insiders\extensions\ms-dotnettools.csharp-1.23.16\.omnisharp\1.37.16\.msbuild\Current\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"

That version of Visual Studio 2019 did not have the .NET 6.0 SDK installed, and I couldn't find it in the VS Installer.那个版本的 Visual Studio 2019 没有安装 .NET 6.0 SDK,我在 VS Installer 中也找不到。

So I installed Visual Studio 2022 and the .NET 6.0 SDK as part of that install.所以我安装了 Visual Studio 2022 和 .NET 6.0 SDK 作为安装的一部分。 Now my log looks like this:现在我的日志如下所示:

[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 3 MSBuild instance(s)
            1: Visual Studio Enterprise 2022 17.0.31912.275 17.0.0 - "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin"
            2: Visual Studio Enterprise 2019 16.8.30907.101 16.8.3 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin"
            3: StandAlone 17.0.0 - "c:\Users\foo\.vscode-insiders\extensions\ms-dotnettools.csharp-1.23.16\.omnisharp\1.37.16\.msbuild\Current\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Visual Studio Enterprise 2022 17.0.31912.275 17.0.0 - "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin"

And the errors are gone!错误消失了!

In my case, Error List window had hundreds of errors.就我而言,错误列表窗口有数百个错误。 But when rebuild the solution, used to get the message Rebuild All Succeeded.但是在重建解决方案时,用于获取消息 Rebuild All Succeeded。 And even the project was able to run.甚至该项目也能够运行。 But entire code was in red.但是整个代码都是红色的。 Just closing Visual Studio and deleting vs folder did the job.只需关闭 Visual Studio 并删除 vs 文件夹即可。

This can also be caused by a circular dependency between projects in a solution.这也可能是由解决方案中项目之间的循环依赖引起的。

error MSB4006: There is a circular dependency in the target dependency graph involving target "_GenerateRestoreProjectPathWalk"错误 MSB4006:在涉及目标“_GenerateRestoreProjectPathWalk”的目标依赖图中存在循环依赖

Just like citelao I've got this issue in VS code when created new .NET 6.0 isolated AZ Function using wizard.就像citelao在使用向导创建新的 .NET 6.0 隔离 AZ 函数时,我在 VS 代码中遇到了这个问题。
In OmniSharp logs I had about the same.在 OmniSharp 日志中,我的情况大致相同。

Just in case, in order to see the omni sharp logs, you have to open 'output' panel, and there on the right click on a drop-down:以防万一,为了查看全向日志,您必须打开“输出”面板,然后右键单击下拉菜单: 在此处输入图像描述

I did not want to install VS 2022 as my target was to work in VS code for this project.我不想安装 VS 2022,因为我的目标是为这个项目使用 VS 代码。 I found the answer here: omnisharp-roslyn #2247我在这里找到了答案: omnisharp-roslyn #2247

Basically the solution was to add omnisharp.json file in the project root with the following contents:基本上解决方案是在项目根目录中添加omnisharp.json文件,内容如下:

{
  "msbuild": {
  "useBundledOnly": true
  }
}

After that I've closed VS Code, deleted bin and obj folders and started VS Code back.之后我关闭了 VS Code,删除了binobj文件夹并重新启动了 VS Code。

Gazi's solution above solved my problem which was exactly what she/he described. Gazi 的上述解决方案解决了我的问题,这正是她/他所描述的。 I had tried updating VS, cleaning and rebuilding solution, restarting VS.我曾尝试更新 VS,清理和重建解决方案,重新启动 VS。 Nothing helped.没有任何帮助。 Removing the hidden vs folder at the solution level did the trick.在解决方案级别删除隐藏的 vs 文件夹就可以了。 Hope it helps someone!希望它可以帮助某人!

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

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