What is the formula in .NET for "*" (wildcard) version numbers?
Asked Answered
S

1

17

Say I have a version number 1.5.* that is compiled as 1.5.4735.28467.

What is the formula for getting 4735 and 28467?

Shiau answered 18/12, 2012 at 23:53 Comment(5)
major, minor, build, revision. Build and revision are up to you....Convoy
blogs.msdn.com/b/jensenh/archive/2005/11/11/491779.aspx. But that's for Microsoft Office; I don't know if it works the same way for the .NET Framework.Outmoded
Build = DateTime.Today.Subtract(new DateTime(2000, 1, 1)).Days; Revision = (int)DateTime.Now.Subtract(DateTime.Today).TotalSeconds / 2; Too bad this got closed; the alleged duplicate is a different question...Hyetography
@Daniel: Is that for the .NET Framework? Do you have a reference?Outmoded
This is what the C# compiler uses when you use [AssemblyVersion("1.5.*")]. I'm not sure if other .NET languages do the same. I can't find any documentation for this; I just compiled something and checked that the numbers match. I don't know where I was remembering the formula from.Hyetography
A
25

Please note: there are alternative tools/rules/schemes/macros/add-ins/etc to control versioning in a different manner; if used they obviously obliterate the following.

However, this is how a "*" version is calculated to .build.revision in a vanilla Visual Studio / .NET build process:

When specifying a version, you have to at least specify major. If you specify major and minor, you can specify an asterisk (*) for build. This will cause build to be equal to the number of days since January 1, 2000 local time, and for revision to be equal to the number of seconds since midnight local time, divided by 2.

See Coding Horror: Determining Build Date the hard way and MSDN: AssemblyVersionAttribute. This default expansion behavior is documented in the AssemblyVersionAttribute class (BCL!) and not special VS compiler behavior (see Daniel's comment) and not exclusively compiler behavior.


Here is one implementation of the formula (as extracted from Daniel's comment):

int Build = DateTime.Today.Subtract(new DateTime(2000, 1, 1)).Days;
int Revision = (int)DateTime.Now.Subtract(DateTime.Today).TotalSeconds / 2; 
Agricola answered 19/12, 2012 at 1:22 Comment(3)
It's most definitely compiler behavior. The compiler doesn't instantiate any attributes; it just encodes the argument values in metadata. But the number gets substituted at compile-time; it must be a special case in the compiler. There's no way to write your a custom attribute to do something similar.Hyetography
@Hyetography That is a good argument. I wish I could find a VS (not .NET) reference on how version numbers worked.Agricola
You can also see the implementation in Roslyn: source.roslyn.codeplex.com/#Microsoft.CodeAnalysis/…Vancevancleave

© 2022 - 2024 — McMap. All rights reserved.