在專案檔中設定組件屬性

您可以使用 MSBuild 屬性,將封裝相關的專案屬性轉換為所產生程式碼檔案中的組件屬性。 此外,您可以使用 MSBuild 項目將任意組件屬性新增至產生的檔案。

使用封裝屬性作為組件屬性

GenerateAssemblyInfoMSBuild 屬性 (Property) 會控制AssemblyInfo專案的屬性 (Attribute) 產生。 當 GenerateAssemblyInfo 值為 true (也是預設值) 時,封裝相關的專案屬性 (Property) 會轉換為組件屬性 (Attribute)。 下表列出產生屬性 (Attribute) 的專案屬性 (Property)。 這也會列出可用來根據每個屬性 (Attribute) 停用該產生的屬性 (Property),例如:

<PropertyGroup>
  <GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
</PropertyGroup>
MSBuild 屬性 Assembly 屬性 停用屬性產生的屬性 (Attribute)
Company AssemblyCompanyAttribute GenerateAssemblyCompanyAttribute
Configuration AssemblyConfigurationAttribute GenerateAssemblyConfigurationAttribute
Copyright AssemblyCopyrightAttribute GenerateAssemblyCopyrightAttribute
Description AssemblyDescriptionAttribute GenerateAssemblyDescriptionAttribute
FileVersion AssemblyFileVersionAttribute GenerateAssemblyFileVersionAttribute
InformationalVersion AssemblyInformationalVersionAttribute GenerateAssemblyInformationalVersionAttribute
Product AssemblyProductAttribute GenerateAssemblyProductAttribute
AssemblyTitle AssemblyTitleAttribute GenerateAssemblyTitleAttribute
AssemblyVersion AssemblyVersionAttribute GenerateAssemblyVersionAttribute
NeutralLanguage NeutralResourcesLanguageAttribute GenerateNeutralResourcesLanguageAttribute

關於這些設定的注意事項:

  • AssemblyVersionFileVersion 預設為 $(Version) 的值,不含尾碼。 例如,如果 $(Version)1.2.3-beta.4,則此值會是 1.2.3
  • InformationalVersion 預設為 $(Version) 的值。
  • 如果 $(SourceRevisionId) 屬性存在,則會附加至 InformationalVersion。 您可以使用 IncludeSourceRevisionInInformationalVersion 停用這種行為 。
  • 也會針對 NuGet 中繼資料使用 CopyrightDescription 屬性。
  • 預設為 DebugConfiguration 會與所有 MSBuild 目標共用。 您可以透過 dotnet 命令的 --configuration 選項進行設定,例如 dotnet pack
  • 建立 NuGet 套件時會使用某些屬性。 如需詳細資訊,請參閱套件屬性

設定任意屬性

也可以將您自己的組件屬性新增至產生的檔案。 若要這樣做,請定義 <AssemblyAttribute> MSBuild 項目,告知 SDK 要建立的屬性型別。 這些項目也應該包含該屬性所需的任何建構函式參數。 例如,System.Reflection.AssemblyMetadataAttribute 屬性具有採用兩個字串的建構函式:

  • 描述任意值的名稱。
  • 要儲存的值。

如果 MSBuild 中的 Date 屬性包含建立組件的日期,則可以使用 AssemblyMetadataAttribute 以使用下列 MSBuild 程式碼,將該日期內嵌至組件屬性:

<ItemGroup>
  <!-- Include must be the fully qualified .NET type name of the Attribute to create. -->
  <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
    <!-- _Parameter1, _Parameter2, etc. correspond to the
        matching parameter of a constructor of that .NET attribute type -->
    <_Parameter1>BuildDate</_Parameter1>
    <_Parameter2>$(Date)</_Parameter2>
  </AssemblyAttribute>
</ItemGroup>

此項目會告知 .NET SDK 發出下列 C# (或對等的 F# 或 Visual Basic) 作為組件層級屬性:

[assembly: System.Reflection.AssemblyMetadataAttribute("BuildDate", "01/19/2024")]

(實際的日期字串是您建置時所提供的任何內容。)

從 .NET Framework 移轉

如果您將 .NET Framework 專案移轉至 .NET 6 或更新版本,可能會遇到與重複組件資訊檔相關的錯誤。 .NET Framework 專案範本會使用組件資訊屬性集來建立程式碼檔案。 檔案通常位於 .\Properties\AssemblyInfo.cs.\Properties\AssemblyInfo.vb。 不過,SDK 樣式專案也會根據專案設定為您產生此檔案。

將程式碼移植到 .NET 6 或更新版本時,請執行下列其中一項:

  • 在專案檔中將 GenerateAssemblyInfo 設定為 false,以停用包含組件資訊屬性的暫存程式碼檔案產生。 這可讓您保留 AssemblyInfo 檔案。
  • AssemblyInfo 檔案中的設定移轉至專案檔,然後刪除 AssemblyInfo 檔案。