“dotnet new sln”是新命令吗?
是的。在 dotnet 命令行界面的 1.0.1 版本中,有一个 dotnet new sln 命令。该命令附带the change from project.json to csproj。如果我们运行dotnet new --help,我们将看到“解决方案文件”作为模板之一。
> dotnet new --help
Templates Short Name Language Tags
----------------------------------------------------------------------
Console Application console [C#], F# Common/Console
Class library classlib [C#], F# Common/Library
Unit Test Project mstest [C#], F# Test/MSTest
xUnit Test Project xunit [C#], F# Test/xUnit
ASP.NET Core Empty web [C#] Web/Empty
ASP.NET Core Web App mvc [C#], F# Web/MVC
ASP.NET Core Web API webapi [C#] Web/WebAPI
Solution File sln Solution
我应该什么时候使用这个?
使用解决方案文件的两次是:
- 当我们想要使用 Visual Studio 时,和/或
- 当我们想将多个项目作为一个单元进行管理时。
创建 .sln 文件而不是只创建项目文件有什么好处?它主要用于在 Visual Studio 中打开吗?我使用 Visual Studio Code for Mac,所以它可能不适用。
不需要 Visual Studio 的好处之一是将多个项目作为一个单元进行管理。
例如,在装有 Visual Studio Code 的 Mac 上,我们可以使用 dotnet CLI 创建新的解决方案、创建一些项目、将这些项目添加到解决方案、恢复解决方案并构建解决方案。
dotnet new sln --name FooBar
dotnet new console --name Foo --output Foo
dotnet new console --name Bar --output Bar
dotnet sln add .\Foo\Foo.csproj
dotnet sln add .\Bar\Bar.csproj
dotnet restore
dotnet build FooBar.sln
调用dotnet build 的最后一个命令具有构建解决方案中所有项目的好处。如果没有解决方案,我们需要在每个项目上调用 dotnet build。
毫无疑问,还有其他不需要使用 Visual Studio 的好处。我把这些留给你去发现。