C# 如何部分加載“超大”解決方案中的部分項目

在有的特有的項目環境下,團隊會將所有的項目使用同一個解決方案進行管理。這種方式方面了管理,但是卻會導致解決方案變得非常龐大,導致加載時間過長。那麼,如何部分加載解決方案中的部分項目呢?就讓我們來借用微軟退出的 slngen 工具來體驗一下部分加載解決方案中的部分項目吧。

slngen 從根項目生成臨時解決方案

SlnGen 是一個 Visual Studio 解決方案文件生成器。Visual Studio 解決方案對於大型項目樹來說通常不能很好地擴展。SlnGen 讀取一個給定項目的項目引用,按需創建一個 Visual Studio 解決方案。例如,你可以針對一個單元測試項目運行 SlnGen,並呈現一個包含單元測試項目及其所有項目引用的 Visual Studio 解決方案。你也可以針對一個有根的文件夾中的遍歷項目運行 SlnGen,打開一個包含你的項目樹的那個視圖的 Visual Studio 解決方案。

安裝 slngen

dotnet tool install --global Microsoft.VisualStudio.SlnGen.Tool --add-source https://api.nuget.org/v3/index.json --ignore-failed-sources

運行以上命令,你就可以在全局安裝 slngen 工具了。然後,你就可以在任何地方使用 slngen 命令了。

slngen --help

最近我們正在組織全新的技術交流方式,歡迎點擊鏈接蒞臨指導 https://www.newbe.pro/links/

爲所有的項目引入 Microsoft.VisualStudio.SlnGen

在你的項目樹中,你需要爲所有的項目引入 Microsoft.VisualStudio.SlnGen 包。可以通過 Directory.Build.props 來輕鬆實現。

<ItemGroup>
  <PackageReference Include="Microsoft.VisualStudio.SlnGen" Version="9.5.2" />
</ItemGroup>

準備一個臨時的測試項目

爲了方便演示,我們創建三個項目,分別是 slngen-demo、slngen-demo-a、slngen-demo-b。

其中,slngen-demo-a 和 slngen-demo-b 項目都引用了 slngen-demo 項目。

mkdir slngen-demo
cd slngen-demo
dotnet new classlib -o slngen-demo
dotnet new console -o slngen-demo-a
dotnet new console -o slngen-demo-b
cd slngen-demo-a
dotnet add reference ../slngen-demo/slngen-demo.csproj
cd ../slngen-demo-b
dotnet add reference ../slngen-demo/slngen-demo.csproj

文件夾結構大致如下:

C:\REPOS\SLNGEN-DEMO
│  Directory.Build.props
│
├─slngen-demo
│  │  Class1.cs
│  │  slngen-demo.csproj
│  │
│  ├─bin
│  └─obj
│
├─slngen-demo-a
│  │  Program.cs
│  │  slngen-demo-a.csproj
│  │
│  ├─bin
│  └─obj
│
└─slngen-demo-b
    │  Program.cs
    │  slngen-demo-b.csproj
    └─obj

使用 slngen 生成臨時解決方案

注意 slngen 是通過驅動 Visual Studio 來生成解決方案的。因此需要在命令行中具備 MSBuild.exe 的路徑。

因此我們需要使用 Developer Command Prompt for VS 2022 來運行 slngen 命令。

我們來使用 slngen 加載 slngen-demo-a 項目。

slngen slngen-demo-a/slngen-demo-a.csproj

通過以上命令,我們就使用 slngen 加載了 slngen-demo-a 項目。

這種方式可以加載 slngen-demo-a 項目和 slngen-demo,但是 slngen-demo-b 項目並沒有被加載。

運行結果大致如下:

C:\Repos\slngen-demo>slngen slngen-demo-a/slngen-demo-a.csproj
SlnGen version 9.5.2+b19739dfbc for .NET Framework
Copyright (c) Microsoft Corporation.  Licensed under the MIT license.

Build started 2/9/2023 8:29:24 PM.
Generating solution for project "C:\Repos\slngen-demo\slngen-demo-a\slngen-demo-a.csproj"
Loading project references...
Loaded 2 project(s) in 840ms
Generating Visual Studio solution "C:\Repos\slngen-demo\slngen-demo-a\slngen-demo-a.sln" ...
Updating existing solution file and reusing Visual Studio cache
Launching Visual Studio...

Success
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.33

總結

通過 slngen,我們可以很方便地加載一個項目及其所有的項目引用。這對於我們在 Visual Studio 中打開一個項目樹的視圖非常有用。可惜 Rider 不得行。

參考資料

感謝您的閱讀,如果您覺得本文有用,請點贊、關注和轉發。最近我們正在組織全新的技術交流方式,歡迎點擊鏈接蒞臨指導 https://www.newbe.pro/links/


  1. https://learn.microsoft.com/visualstudio/msbuild/customize-your-build?view=vs-2022&WT.mc_id=DX-MVP-5003606#directorybuildprops-example

  2. https://www.nuget.org/packages/Microsoft.VisualStudio.SlnGen.Tool

  3. https://github.com/microsoft/slngen

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章