[英]How do push 2 projects in the same solution to my Github repository?
[英]How do I automatically set all projects in my solution to the same version?
我有一個包含 5 個 C# 項目的 Visual Studio 解決方案。 我希望程序集版本在每個項目中都匹配。 但看起來我必須將 go 放入每個項目的屬性中,然后一次單擊每個項目的程序集版本。 有沒有辦法將解決方案視為只有一個適用於每個項目的整體版本?
只需在解決方案根文件夾中創建一個文件,例如GlobalAssemblyInfo.cs
,然后向其中添加必要的屬性,最后將其作為現有項目作為鏈接添加到每個項目中。
在Solution Explorer
右鍵單擊project name > Add > Existing item...
然后在對話框中從下拉列表中選擇Add As Link
選項,如您在此圖像中所見。
// Content of GlobalAssemblyInfo.cs file
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
請注意:
Properties\\AssemblyInfo.cs
文件中刪除這些屬性。GlobalAssemblyInfo.cs
文件中結果是您將只有一個可以設置版本的文件,它將適用於所有項目。
更新#1:
在.NET 5
項目中,默認情況下,在構建期間會自動生成AssemblyInfo.cs
文件。
好像只有7個屬性是自動生成的:
AssemblyCompanyAttribute
AssemblyProductAttribute
AssemblyConfigurationAttribute
AssemblyVersionAttribute
AssemblyFileVersionAttribute
AssemblyInformationalVersionAttribute
AssemblyTitleAttribute
您在這里有兩個選擇:
AssemblyInfo.cs
文件的自動生成。AssemblyInfo.cs
文件的自動生成並關閉特定屬性的生成。 創建一個文件(名稱: Directory.Build.props
)並將其放在.sln
文件旁邊,以便將其應用於解決方案中的所有項目。
示例 #1 - 禁用AssemblyInfo.cs
文件的自動構建
Directory.Build.props
:
<Project>
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
</Project>
示例 #2 - 僅禁用特定屬性生成
在這種情況下,只需添加<Generate...>false</Generate...>
行以禁用特定屬性,其中...
是屬性類型名稱。
Directory.Build.props
:
<Project>
<PropertyGroup>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
</PropertyGroup>
</Project>
備注
了解有關 SDK 樣式項目文件中的AssemblyInfo 屬性的更多信息。
此更新也適用於 .NET Core 版本。
如果特定項目有特殊需求,您也可以覆蓋.csproj
文件中的這些設置。
至於我,我通常把屬性如下:
GlobalAssemblyInfo.cs
AssemblyCompanyAttribute
AssemblyProductAttribute
AssemblyCopyrightAttribute
AssemblyConfigurationAttribute
AssemblyTrademarkAttribute
AssemblyCultureAttribute
AssemblyVersionAttribute
AssemblyFileVersionAttribute
AssemblyInformationalVersionAttribute
ComVisibleAttribute
AssemblyInfo.cs
(在特定項目中)
AssemblyTitleAttribute
AssemblyDescriptionAttribute
GuidAttribute
我認為沒有任何解決方案級別的選項可以做到這一點。 我使用 powershell 腳本在一個解決方案中為我的 15 個項目實現它。
$version= "1.3.0.0"
(Get-ChildItem -Include AssemblyInfo.cs -Recurse ) |
Foreach-Object {
Set-Content $_ ((Get-content $_ -Encoding UTF8) -replace "\d+\.\d+\.(\d+|\*)(\.(\d+|\*))?", $version) -Encoding UTF8
}
將此腳本保存在與解決方案文件相同的目錄中。 您還可以將其添加為解決方案本身中的解決方案項,並在您右鍵單擊腳本文件時從 Visual Studio 命令行選項啟動它。
由於每個項目都有自己的程序集,因此它們將被獨立處理。 我發現最適合我的是使用 T4 模板並基於模板中的算法,例如這樣的修訂是自 2000 年 1 月 1 日以來的幾個小時的計算:
<#@ template debug="false" hostspecific="false" language="C#" #>
using System.Reflection;
[assembly: AssemblyVersion("1.0.6.<#= this.RevisionNumber #>")]
[assembly: AssemblyFileVersion("1.0.6.<#= this.RevisionNumber #>")]
<#+
int RevisionNumber = (int)(DateTime.UtcNow - new DateTime(2000,1,1)).TotalHours;
#>
由於 MSBuild 15 (Visual Studio 2017) ( https://docs.microsoft.com/en-us/visualstudio/msbuild/what-s-new-in-msbuild-15-0 ) 你可以使用Directory.Build.targets
文件為了這。
Directory.Build.targets
文件 <Project>
<PropertyGroup>
<Version>0.1.0-alpha</Version>
</PropertyGroup>
</Project>
您可以使用 Directory.Build.props 和 Directory.Build.targets 做許多其他事情 - 更多內容:( https://docs.microsoft.com/en-us/visualstudio/msbuild/customize-your-build )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.