【问题标题】:Do I need AssemblyInfo while working with .NET Core?使用 .NET Core 时是否需要 AssemblyInfo?
【发布时间】:2017-01-02 23:09:14
【问题描述】:

以前,AssemblyInfo.cs 文件由 Visual Studio 自动创建以包含程序集范围的属性,如 AssemblyVersion、AssemblyName 等。

在 .NET Core 和 ASP.NET Core 中,project.json 负责保存大部分信息。

所以问题是:我是否需要再用该属性标记我的程序集? 如果我不使用该属性标记程序集,我会陷入什么陷阱?

【问题讨论】:

  • 您能否更具体地说明您错过了哪些属性以及您害怕什么?
  • 我发现属性实际上是多余的:AssemblyVersion(version)、AssemblyTitle(title)、AssemblyDescription(copyright)。当这些程序集将用于 UWP 或跨平台解决方案时,我害怕计算出一些所需的属性。一般来说,我只想继续使用project.json

标签: asp.net-core .net-core csproj assemblyinfo


【解决方案1】:

project.json 已替换 AssemblyInfo

AssemblyVersionAttribute 替换为 version 属性

version
Type: String
The Semver version of the project, also used for the NuGet package.

AssemblyNameAttribute 现在是 name 属性

name
Type: String
The name of the project, used for the assembly name as well as the name of the package. The top level folder name is used if this property is not specified.

等等


更新:随着 .NET Core Tools MSBuild 的发布,.csproj 已取代 project.jsonAssemblyInfo.cs 文件又回来了,但大部分设置已直接移至.csproj。有关详细信息,请参阅相关的 SO 问题:Equivalent to AssemblyInfo in dotnet core/csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net461</TargetFramework>
    <Version>1.2.3.4</Version>
    <Authors>Author 1</Authors>
    <Company>Company XYZ</Company>
    <Product>Product 2</Product>
    <PackageId>MyApp</PackageId>
    <AssemblyVersion>2.0.0.0</AssemblyVersion>
    <FileVersion>3.0.0.0</FileVersion>
    <NeutralLanguage>en</NeutralLanguage>
    <Description>Description here</Description>
    <Copyright>Copyright</Copyright>
    <PackageLicenseUrl>License URL</PackageLicenseUrl>
    <PackageProjectUrl>Project URL</PackageProjectUrl>
    <PackageIconUrl>Icon URL</PackageIconUrl>
    <RepositoryUrl>Repo URL</RepositoryUrl>
    <RepositoryType>Repo type</RepositoryType>
    <PackageTags>Tags</PackageTags>
    <PackageReleaseNotes>Release</PackageReleaseNotes>
  </PropertyGroup>

【讨论】:

    【解决方案2】:

    project.json 不是AssemblyInfo.cs 的直接替代品,因此如果您想定义一些您无法在project.json 中提供的值,仍然需要。

    从问题https://github.com/aspnet/dnx/issues/2715 可以看到,在开始时,一些参数,如titledescriptioncopyright 等用于填充生成的nuget 包的字段。随着问题 2715 的诞生,这个想法诞生了,这些价值观可以“流入大会”。这样您就不必在两个不同的地方配置这些字段。因此,如果您不想配置超过这些参数,则不需要AssemblyInfo.cs

    [InternalsVisibleTo] 等其他字段无法在project.json 中配置。所以在某些情况下,仍然需要定义一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-29
      • 2021-07-11
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2018-03-16
      • 2011-06-28
      • 2021-08-21
      相关资源
      最近更新 更多