我注意到在新的.NET Core项目中没有创建AssemblyInfo.cs
文件。我发现你仍然可以设置程序集属性,例如AssemblyVersion
等。
是否仍然有使用AssemblyInfo.cs
文件的有效理由?
您完全可以创建一个 AssemblyInfo.cs
文件,并像以前一样配置您的程序集。当然,由于使用程序集属性设置属性,您不需要使用 AssemblyInfo
,而可以选择任何其他文件名,甚至是现有文件。
话虽如此,AssemblyInfo.cs
不再包括在默认模板中的原因是新的 SDK 样式项目类型支持在 csproj
项目文件中设置此信息。
因此,设置程序集版本的通常方法是在项目文件中设置 Version
属性(或作为构建过程的一部分自动设置)。例如:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Version>1.2.3</Version>
</PropertyGroup>
…
</Project>
由于这是一个MSBuild属性,您还可以在构建过程中设置它,例如使用 dotnet build /p:Version = 1.2.3
命令。
此外,还有 VersionPrefix
和 VersionSuffix
属性,可用于从环境(例如Git提交ID或构建号)自动构建版本号。
除了与版本相关的属性外,还可以在项目文件中设置一些其他NuGet属性,这样将使 AssemblyInfo.cs
文件基本上变得多余。
根据迁移指南,现在我们有很少灵活的方法来设置程序集属性。
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("...")]
[assembly: System.Reflection.AssemblyCopyrightAttribute("...")]
[assembly: System.Reflection.AssemblyDescriptionAttribute("...")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("...")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("...")]
[assembly: System.Reflection.AssemblyProductAttribute("...")]
[assembly: System.Reflection.AssemblyTitleAttribute("...")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0-dev01234567")]
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<Company>...</Company>
<Copyright>...</Copyright>
<Description>...</Description>
<Product>...</Product>
<AssemblyTitle>...</AssemblyTitle>
<Version>1.0.0-dev01234567</Version>
</PropertyGroup>
...
</Project>
注意:您可以合并两个解决方案,但避免重复使用程序集属性。我正在使用Visual Studio 2022和.NET Core 6.
这是一个包含“Version”、“AssemblyVersion”和“FileVersion”的示例项目文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
</PropertyGroup>
...
</Project>
version
设置有什么区别? - JobaDiniz[AssembyAttributes]
,但它不支持自动生成任意[AssembyAttributes]
或其他元数据用于程序集。
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
。 - Jesse Chisholm