在 .NET 6 中使用 dotnet format 格式化代碼
我不得不承認,在 code review 的時候,我花費了很多時間來研究 C# 的代碼格式問題,這是沒有太大意義的工作,我應該專注于其他事情,而不是觀察同事是否忘記格式化代碼,或者是使用了其他不同規(guī)則的代碼編輯器。
現(xiàn)在,我們可以使用 dotnet format 檢查代碼格式問題。這是 .NET 6 的新功能,.NET CLI 引入了 dotnet format 命令 ,并且它是內(nèi)置的命令。
需要注意的是,如果你安裝了 .NET 6 Preview 7 SDK 或更高版本,應該使用 dotnet-format 而不是 dotnet format, 否則運行命令會報錯。
如何使用?
格式化解決方案中的所有代碼:
dotnet format ./solution.sln格式化單個項目的代碼
dotnet format ./src/application.csproj驗證所有代碼的格式是否正確:
dotnet format --verify-no-changes格式化 src 和 tests 目錄中的所有代碼,并排除 src/submodule-a 目錄的代碼:
dotnet format --include ./src/ ./tests/ --exclude ./src/submodule-a/格式化并輸出詳細信息
dotnet-format -v diag格式化并記錄報告到文件
dotnet format --report ./report.json更多 dotnet-format 的信息,可以查看下面的鏈接
https://github.com/dotnet/format
做了一個 .NET 的學習網(wǎng)站,內(nèi)容涵蓋了 分布式系統(tǒng),數(shù)據(jù)結構與算法,設計模式,操作系統(tǒng),計算機網(wǎng)絡等, 回復 dotnet 獲取網(wǎng)站地址, 回復 面試題 獲取 .NET 面試題。

評論
圖片
表情
