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 8632413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:20:57+00:00 2026-06-12T09:20:57+00:00

Our control system has a lot of data files that have specific versions. The

  • 0

Our control system has a lot of data files that have specific versions. The data files are “append only”, so a higher version will always contain a superset of the definitions in the earlier versions.

In order to avoid “magic numbers” in our code, we have created a code generator that take a piece of data and turns it into a C# class file. I have a pretty large class library containing pretty much only such files. This results in much more readable code and some additional compiler checks because type previously risky type casting can now be part of the generated code, so that users of the library consume their correct subtypes directly.

However, the question comes up what data file version was used in the latest version of the class library. I would like every class file to “contribute” to a global list. If static constructors were automatically run, this is what I would do:

// GlobalConstants.cs (not generated)
public static partial class GlobalConstants
{
    public static List<string> Lst = new List<string>(); 
    static void AddVersionInfo(string mod)
    {
        Lst.Add(mod);
    }
}

// Motor.cs
public static partial class GlobalConstants
{
    // Generated from Motor metadata
    public class MotorUtil
    {            
        public const int SomeConst1 = 2;
        static MotorUtil()
        {
            AddVersionInfo("<Motor version info>");
        }
    }
}

// Tank.cs
public static partial class GlobalConstants
{
    // Generated from Tank metadata        
    public class TankUtil
    {
        public const int SomeConst1 = 2;
        public const int SomeConst2 = 3;
        public const int SomeConst3 = 5;
        static TankUtil()
        {
            AddVersionInfo("<Tank version info>");
        }
    }
}

If static constructors where run automatically, this would result in every generated file (tank.cs, motor.cs, boiler.cs etc) being added to the list, which could be inspected by a tool.

A compile time MEF, if you like.

Unfortunately. If I print out contents of “Lst”, then it’s always an empty list because the static constructors haven’t run.

These .cs files come from different parties and the author of GlobalConstants.cs should not be required to know about them.
My question is: Is there a way in which I can generate this code so that one can add generated .cs files at will and they will appear in the “Lst” list?

  • 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-12T09:20:58+00:00Added an answer on June 12, 2026 at 9:20 am

    The static constructors for each of your nested classes won’t run until those nested classes are referenced for the first time. That hasn’t happened in your above code, hence the list is empty.

    One way I could imagine you might be able to do this would be for a static initializer in the GlobalConstants class to use reflection to find the nested classes.

    Or maybe you could add a static field in each partial class file that references the nested class, forcing its static constructor to run:

    public static partial class GlobalConstants
    {
        // Generated from Tank metadata        
    
        private static TankUtil _unusedTankUtilField = new TankUtil();
    
        public class TankUtil
        {
            public const int SomeConst1 = 2;
            public const int SomeConst2 = 3;
            public const int SomeConst3 = 5;
            static TankUtil()
            {
                AddVersionInfo("<Tank version info>");
            }
        }
    }
    

    Also I don’t believe the order of execution of static initializers across partial class files is strictly defined (presumably corresponds to the order they are processed by the compiler). So I’d suggest you make your “AddVersionInfo” method responsible for initializing the list:

    public static partial class GlobalConstants
    {
        public static List<string> Lst; 
        static void AddVersionInfo(string mod)
        {
            if (Lst == null) Lst = new List<string>();
            Lst.Add(mod);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using TFS as version control system for our software. Currently the software has
We have a dynamic languaging system that doesn't use conventional resource files; fetching resources
I have been hearing a lot about the advantages of using a version control
I'm researching using Mercurial for our companies source control system. One immediate requirement that
Possible Duplicate: What is a version control system? How is Git useful? We have
We use Subversion as our source control system and store the VisualStudio project files
In our backoffice we have a core-system that generates models from the DB. When
My company is in the process of changing our version control tool from Rational
in our application we have a Java applet running inside a .NET browser control.
We have a requirement to control access to our SaaS based web application based

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.