简体   繁体   English

从程序集属性中获取 Nuget package 属性(在 Visual Studio 2022 中)

[英]Get Nuget package properties from assembly attributes (in Visual Studio 2022)

I have a C# project in Visual Studio 2022.我在 Visual Studio 2022 中有一个 C# 项目。
In the AssemblyInfo.cs file I have an AssemblyVersion attribute:AssemblyInfo.cs文件中,我有一个AssemblyVersion属性:

using System.Reflection;
[assembly: AssemblyVersion("1.2.3")

I want to generate a NuGet package automatically on build, and I was wondering if it would be possible to specify, in the package properties dialog, to use the AssemblyVersion as package version, to avoid having to remember to change the version in two places each time.我想在构建时自动生成一个 NuGet package,我想知道是否可以在 package 属性对话框中指定使用AssemblyVersion作为 package 版本,以避免必须记住分别在两个地方更改版本时间。

Something like this (which doesn't work):像这样的东西(不起作用):

包属性

Is there a way to do this?有没有办法做到这一点?

The $(AssemblyVersion) value is trying to reference an MSBuild property. $(AssemblyVersion)值试图引用 MSBuild 属性。 It cannot see an [AssemblyVersion] attribute in your code at build time.它在构建时看不到代码中的[AssemblyVersion]属性。 I recommend you move the specification of the version to your project file (search for "version" in that UI, or edit your .csproj and set the <Version> property manually. In that way you only have to set the value once; the version will be set on the assembly, and used during pack operations.我建议您将版本规范移至您的项目文件(在该 UI 中搜索“版本”,或编辑您的.csproj并手动设置<Version>属性。这样您只需设置一次值; version 将在程序集上设置,并在打包操作期间使用。

You can remove the AssemblyInfo and specify the version in your csproj file (see below).您可以删除AssemblyInfo并在 csproj 文件中指定版本(见下文)。 do.net pack then should produce both package and dll with correct version. do.net pack然后应该生成正确版本的 package 和 dll。

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>

        ...

        <Version>MAJOR.MINOR.PATCH</Version>
        <IsPackable>true</IsPackable>
    </PropertyGroup>

    ...

</Project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM