简体   繁体   中英

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

I have a C# project in Visual Studio 2022.
In the AssemblyInfo.cs file I have an AssemblyVersion attribute:

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.

Something like this (which doesn't work):

包属性

Is there a way to do this?

The $(AssemblyVersion) value is trying to reference an MSBuild property. It cannot see an [AssemblyVersion] attribute in your code at build time. 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.

You can remove the AssemblyInfo and specify the version in your csproj file (see below). do.net pack then should produce both package and dll with correct version.

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

        ...

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

    ...

</Project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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