Q:

Attempting to create Auto-Versioning System in C# w/Visual Studio 2010

So I have a large collection of classes, and when a class is updated I need to run an update method on any existing saved data. Currently I use something like:
public const decimal LatestVersion = 0.0121201313m;
private decimal LastVersion;

//...

if (Abilities.LastVersion != Abilities.LatestVersion)
{
    //...

And I manually update the LatestVersion number every time I update the class... I also have to repeat this method in every class that may need to be updated. It works well, but I have to remember to update the version number.
What I am thinking would be nice is to have the LatestVersion automatically update to the current DateTime (maybe via a debug command) if the class was changed or something like that... But I don't think that is possible.
Does anyone have any other ideas on how I can setup a auto-implementing versioning system on a class level?

A:

Off the top of my head, either create a transform-on-build T4 that builds a class/dictionary of .cs/type names to hash/timestamp or alternatively tokenize that property and hook into MSBuild's BeforeBuild event to replace it before compilation with similar hash/lastmodified.
Edit: Ok, so here are a couple of very crude examples.
T4 -- Create a Text Template in your project (.tt) and paste the following
<#@ template hostspecific="true" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
namespace ClassLibrary1
{
    public class Version
    {
        <#  var project = new FileInfo(Host.ResolvePath("ClassLibrary1.csproj"));
            WriteLine("// " + project.FullName);

            var files = project.Directory.GetFileSystemInfos("*.cs", SearchOption.AllDirectories);
            foreach (var file in files)
            {
                var name = Path.GetFileNameWithoutExtension(file.Name);
                if (name == "Version")
                    continue; #>
               public const long <#= name #> = <#= file.LastWriteTime.Ticks #>;
        <#  } #>
    }
}

You'll now have a class that you can use in comparisons via Version.ClassName constants.
MSBuild -- Edit your .csproj and add the following at the end.
<UsingTask TaskName="Version" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  <ParameterGroup>
    <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
  </ParameterGroup>
  <Task>
    <Using Namespace="System.IO" />
    <Using Namespace="System.Text.RegularExpressions" />
    <Code Type="Fragment" Language="cs"><![CDATA[
          var regex = new Regex(@"Version = (\d+);//(-?\d+)");
          foreach (var file in Files)
          {
              var source = File.ReadAllText(file.ItemSpec);
              var match = regex.Match(source);
              if (!match.Success) continue;
              var hash = regex.Replace(source, string.Empty).GetHashCode();
              if (int.Parse(match.Groups[2].Value) == hash) continue;
              source = regex.Replace(source, string.Format("Version = {0};//{1}", int.Parse(match.Groups[1].Value) + 1, hash));
              File.WriteAllText(file.ItemSpec, source);
          }
      ]]></Code>
  </Task>
</UsingTask>
<Target Name="BeforeBuild">
  <Version Files="@(Compile)" />
</Target>

Then add public const int Version = 0;//0 to your class. Before compilation MSBuild will get hash of the file and update the counter if previous one doesn't match.
There is a crapton that can be done better in these examples, but hopefully they'll illustrate the directions you can take. May be there is a better way, those are first two ideas that poped into my head after hashing IL with Mono.Cecil, I found the question quite interesting. If you do go either route I recommend reading into T4 and its Host and how to force regeneration on build, or MSBuild and extending events, custom and inline tasks, maybe even go into EnvDTE, CodeDOM, Microsoft.Build.Evaluation namespaces, etc rather than just searching for .cs files and regexing.

