Search code examples
c#.netassemblyversionattribute

How to avoid repeated constant string in .NET AssemblyVersion and AssemblyFileVersion


In a C# project, we increment versions manually and have a top-level file MyAssemblyInfo.cs containing

[assembly: AssemblyVersion("31.1.13")]
[assembly: AssemblyFileVersion("31.1.13")]
[assembly: AssemblyInformationalVersion("31.1.13-V1.0.0")]

I'd like to avoid repeating the version. For example, with the C preprocessor, one could write this as

#define VERSION "31.1.13"
[assembly: AssemblyVersion(VERSION)]
[assembly: AssemblyFileVersion(VERSION)]
[assembly: AssemblyInformationalVersion(VERSION "-V1.0.0")]

Is there a way to achieve this in C# without using any external tools?


Solution

  • [assembly: AssemblyFileVersion(AppVersion.Version)]
    [assembly: AssemblyInformationalVersion(AppVersion.Version)]
    // you can use + here but the format you use shows as invalid version for me
    [assembly: AssemblyVersion(AppVersion.Version)] 
    
    // this must be at the end of the global declarations
    internal class AppVersion
    {
        public const string Version = "31.1.13";
    }