對于帶有 Visual Studio 2019 的 ASP.Net,我創建了一個共享的 AssemblyInfo.cs 檔案,然后在解決方案的每個專案中都參考了該檔案。從而給我一個位置來設定版本資訊,然后為每個專案設定。
using System.Reflection;
using System.Runtime.InteropServices;
using JetBrains.Annotations;
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Company Name")]
[assembly: AssemblyProduct("Product name")]
[assembly: AssemblyCopyright("Copyright ? Company. All Rights Reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.15.0")]
[assembly: AssemblyFileVersion("1.15.0.7156")]
[assembly: AssemblyInformationalVersion("1.15.0")]
每個專案都有一個簡化的 AssemblyInfo.cs 檔案:
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ProjectName")]
[assembly: AssemblyDescription("")]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
并且共享的 AssemblyInfo.cs 檔案作為鏈接包含在每個專案中。
我正在使用 Visual Studio 2022 和 .Net Core 6 開始一個新的解決方案。我看到我可以在每個專案的 csproj 中設定 Version、AssemblyVersion 和 FileVersion ......但我看不出我如何才能擁有一個中央像我在上一個解決方案中所做的那樣設定值的地方。
我已經看到我可以恢復為 AssemblyInfo.cs 檔案的手動創建,但是有沒有辦法在不禁用當前標準方式的情況下做我想做的事情(在 VS2022 / .Net Core 6 中)?
代碼處于源代碼控制 (SVN) 之下,但我們(當前)不使用任何構建或 CI 工具。我只是希望能夠將版本設定為特定值,并使其對解決方案中的所有專案都相同。我很感激我可以打開每個 csproj 檔案并更新它們,但我寧愿有一個位置。
uj5u.com熱心網友回復:
您可以使用 MSBuild 匯入在每個 csproj 中包含一個公共檔案。例如,創建一個包含所有共享屬性的檔案:
<Project>
<PropertyGroup>
<AssemblyVersion>1.15.0</AssemblyVersion>
<AssemblyFileVersion>1.15.0.7156</AssemblyFileVersion>
<!-- etc... -->
</PropertyGroup>
</Project>
然后您可以<Import Project="CommonAssemblyProperties.props" />(或您命名檔案的任何名稱)在每個專案檔案中。
如果要自動匯入,可以將其放在解決方案根目錄下的 Directory.Build.props 檔案中,因為該檔案是自動匯入的(您甚至可以在此處分配屬性,而不必<Import>使用額外的檔案)。
如果需要(例如,如果您希望 CI 構建與開發構建不同),這種方法還允許您在構建期間覆寫屬性值msbuild MySolution.sln /p:Property=Value,或者通過在 csproj 中設定屬性值在單個專案中(例如,如果一個專案有與其他版本不同的特殊版本)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/429857.html
