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

Time:01-23

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):

Package properties

Is there a way to do this?

CodePudding user response:

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.

CodePudding user response:

You can remove the AssemblyInfo and specify the version in your csproj file (see below). dotnet 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>
  • Related