首页 > 解决方案 > vscode omnisharp 不适用于解决方案,但适用于项目

问题描述

我的文件夹结构如下: project/csproj A, project/csproj B 如果我在 vs code 项目目录中打开,omnisharp 仅适用于 csproj B。如果我在 vs code project/csproj 目录中打开,omnisharp 对这个项目有效。

如何解决这个问题,以便我可以打开项目目录并为两个项目使用全功能?

如果重要的话,破碎的项目是 nunit 类型。我尝试重新加载 vscode,禁用/启用omnisharp。我在空目录中创建了项目

dotnet new nunit -n=projectB

标签: visual-studio-code.net-coreomnisharp

解决方案


解决方案由 sln 文件指定,如果您还没有,只需在解决方案级文件夹中创建一个新的

dotnet new sln

然后,您必须将项目引用添加到解决方案

dotnet sln path/to/solution add path/to/project

如果omnisharp没有更新它,重新启动它或vs代码。

Omnisharp 只能支持单个项目或解决方案,因此,要支持多个项目,您必须使用“解决方案”,它不仅仅是一个文件夹,而是一个 sln 文件。

您的文件夹结构是反模式的,自然不支持,因为 .NET Core 使用 SDK 样式的 csproj 项目,它将所有源文件添加到项目级文件夹(这是 csproj 文件所在的位置),因此一个项目中有多个 csproj 文件-level 文件夹仅适用于具有多个目标的一个项目。如果您的项目 A 和 B 在同一个文件夹中,则意味着它们可能包含重复的源文件,这可能会导致类型冲突的错误,除非您在 csproj 文件中分别指定了源排除。

推荐的文件夹结构是

<Solution and git repository level folder>
 |-- src
 |    |-- <Project level folders>
 |    |    |-- <Folder structures based on namespace>
 |    |    |    └-- <Source files>
 |    |    |-- <Asset files with approprate folder structure>
 |    |    └-- <The csproj file>
 |    |-- Directory.Build.props (Common MSBuild props for all src projects)
 |    └-- Directory.Build.targets (Common MSBuild targets for all src projects)
 |-- test
 |--  └-- <Test projects with similar folder structure to src>
 |-- build
 |--  └-- <Common MSBuild props and targets files to be referenced by src and test>
 |-- docs
 |    └-- <Documents>
 |-- <Other repository assets>
 └-- <The sln file>

推荐阅读