null_ptr
null_ptr

Reputation: 1

How to retrieve AssemblyVersionAttribute in MSBuild script

When trying to retrieve the [assembly: AssemblyVersion("1.2.3.4")] present in AssemblyInfo.cs file from a custom MSBuild script, I always get $(Version) value 1.0.0.0 instead of the value written in the file.

Can somebody tell me how to fix this issue? Unfortunately, other entries I have found so far, didn't point me to the right direction.

Upvotes: 0

Views: 482

Answers (1)

Mr Qian
Mr Qian

Reputation: 23715

Actually, you just want to get the assembly dll version by MSBuild rather the nuget package version.

$(Version) is the value of the nuget package version after you pack the lib project. See this official document.

If you want to get the internal assembly dll version, it can be a bit complex but it can be done with some MSBuild tasks.

Use this:

<Target Name="RetrieveIdentities" BeforeTargets="Build">
        <GetAssemblyIdentity
            AssemblyFiles="$(TargetPath)">
            <Output
                TaskParameter="Assemblies"
                ItemName="MyAssemblyIdentities"/>           
        </GetAssemblyIdentity>
        
        <Message Text="Version: %(MyAssemblyIdentities.Version)"/>
            
</Target>

And %(MyAssemblyIdentities.Version) is the value of the AssemblyVersion.

Upvotes: 1

Related Questions