在项目文件中设置程序集属性

可以使用 MSBuild 属性将 包相关的项目属性转换为 生成的代码文件中的程序集属性。 此外,可以使用 MSBuild 项将 任意程序集属性 添加到生成的文件中。

将包属性用作程序集属性

GenerateAssemblyInfo MSBuild 属性控制AssemblyInfo项目的属性生成。 GenerateAssemblyInfo当值为true(即默认值)时,包相关的项目属性将转换为程序集属性。 下表列出了生成属性的项目属性。 还列出了可用于基于每个特性禁用该生成的属性,例如:

<PropertyGroup>
  <GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
</PropertyGroup>
MSBuild 属性 程序集属性 要禁用特性生成的属性
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 禁用此行为。
  • CopyrightDescription 属性也用于 NuGet 元数据。
  • Configuration 的默认值是 Debug ,它与所有 MSBuild 目标共享。 你可以通过--configuration命令的dotnet选项来设置它,例如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")]

(实际日期字符串是你在生成时提供的任何字符串。)

如果特性具有其他 System.String参数类型,则可以通过使用 MSBuild WriteCodeFragment 任务支持的 XML 元素的特定模式来指定参数。 请参阅 WriteCodeFragment 任务 - 生成程序集级属性

从 .NET Framework 迁移

如果将 .NET Framework 项目迁移到 .NET 6 或更高版本,则可能会遇到与重复程序集信息文件相关的错误。 这是因为 .NET Framework 项目模板创建包含程序集信息属性集的代码文件。 该文件通常位于 .\Properties\AssemblyInfo.cs.\Properties\AssemblyInfo.vb。 但是,SDK 样式项目还会根据项目设置为你 生成 此文件。

将代码移植到 .NET 6 或更高版本时,请执行以下作之一:

  • 通过在项目文件中设置GenerateAssemblyInfofalse,禁用包含程序集信息属性的临时代码文件的生成。 这使你可以保留 AssemblyInfo 文件。
  • AssemblyInfo 文件中的设置迁移到项目文件,然后删除 AssemblyInfo 文件。