One approach which I'm amazed no-one has mentioned yet is to use T4 Text Templates for code generation.
<#@ template debug="false" hostspecific="true" language="C#" #> <#@ assembly name="System.Core" #> <#@ import namespace="System" #> <#@ output extension=".g.cs" #> using System; namespace Foo.Bar { public static partial class Constants { public static DateTime CompilationTimestampUtc { get { return new DateTime(<# Write(DateTime.UtcNow.Ticks.ToString()); #>L, DateTimeKind.Utc); } } } } Pros:
- Locale-independent
- Allows a lot more than just the time of compilation
Cons:
- Only applicable to libraries where you control the source
- Requires configuring your project (and build server, if that doesn't pick it up) to execute the template in a pre-build step. (See also T4 without VS).