Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:12:15+00:00 2026-06-17T20:12:15+00:00

So I have a large collection of classes, and when a class is updated

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:12:16+00:00Added an answer on June 17, 2026 at 8:12 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a large internal data collection website. I don't have time to create
I have a large amount of classes for: a main forums class, a forum
I have a large collection of data chunks sized 1kB (in the order of
We have a large collection of DTS packages that need to be converted to
I have a large amount of data stored in a Collection . I would
I have a dataset consisting of a large collection of points in three dimensional
I have a large (more than 100K objects) collection of Java objects like below.
I have a large script that has no database connection yet. I need one
I have a large collection of unique strings (about 500k). Each string is associated
If I have a large collection (40m+ objects) and I'm looking to find: (a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.