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?
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
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.
Then add
public const int Version = 0;//0to 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
Hostand how to force regeneration on build, or MSBuild and extending events, custom and inline tasks, maybe even go intoEnvDTE,CodeDOM,Microsoft.Build.Evaluationnamespaces, etc rather than just searching for .cs files and regexing.