This might be a decade too late to be useful, but maybe it'll help someone else.
You can add this target to your csproj file, just paste it in at the end:
<Target Name="BuildDate" BeforeTargets="CoreCompile">
<PropertyGroup>
<SharedAssemblyInfoFile>$(IntermediateOutputPath)CustomAssemblyInfo.cs</SharedAssemblyInfoFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(SharedAssemblyInfoFile)" />
</ItemGroup>
<ItemGroup>
<AssemblyAttributes Include="AssemblyMetadata">
<_Parameter1>AssemblyDate</_Parameter1>
<_Parameter2>$([System.DateTime]::UtcNow.ToString("u"))</_Parameter2>
</AssemblyAttributes>
</ItemGroup>
<WriteCodeFragment Language="C#" OutputFile="$(SharedAssemblyInfoFile)" AssemblyAttributes="@(AssemblyAttributes)" />
</Target>
This will create a CustomAssemblyInfo.cs file in your "obj" folder, that contains a single line: [assembly: AssemblyMetadata("AssemblyDate", "DATE_OF_BUILD_HERE")]
This file will be compiled into your assembly, so you can access it at runtime using reflection, such as with the following code:
using System;
using System.Linq;
using System.Reflection;
public class Application
{
static DateTime BuildDate;
public static DateTime GetBuildDate()
{
if (BuildDate == default(DateTime)) {
var attr = typeof(Application).Assembly.GetCustomAttributes<AssemblyMetadataAttribute>();
var dateStr = attr.Single(a => a.Key == "AssemblyDate")?.Value;
BuildDate = DateTime.Parse(dateStr);
}
return BuildDate;
}
}
One thing to note, the AssemblyMetadataAttribute that I'm using here was only added in .NET 4.5.3, so if you need to support a version previous to that you can simply define your own custom attribute type to hold the date value.