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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:34:15+00:00 2026-06-02T09:34:15+00:00

I have built an abstract class that is used to handle command line options

  • 0

I have built an abstract class that is used to handle command line options for our products.

One need only to create a class inheriting from AbstractOptions, fill it with decorated fields and call the inherited Parse(args) method to have it automatically filled through reflection with values from the command line. Values that were not found on the command line retain their current (default) values.

Then, the application needs only to check the option fields to get their value. The AbstractOptions class provides more features, like help output, etc, but it is beside the point.

Short example:

public class SignalOptions: AbstractOptions
{
    [Option("-i, --iterations", "Number of iterations (0 = infinite).")]
    volatile public int NumberOfIterations;

    [Option("-s, --silent", "Silent mode, only display final results.")]
    volatile public bool Silent;

    [Option("-w, --zwindow", "Window size for z-score analysis.")]
    volatile public int ZWindow = 7;

    [Option("-a, --zalert", "z-score value to consider as peak.")]
    public double ZAlert = 2.1;
}

static void Main(string[] args)
{
    var opts = new SignalOptions();
    opts.Parse(args)

    // If optimizations are turned off, SILENT will be written or not 
    // followind presence or absence of the --silent switch on the command line.
    // If optimizations are turned on, SILENT will never be written.
    // The reflection part is working fine. I suspect the problem is that
    // the compiler of the jitter having never found this set anywhere simply inlines
    // the value 'false' inthe if, because when I step on it, it shows me the value as 
    // true or false, but has the same behavior no matter what value opts.Silence has.
    if( opts.Silent )
        Console.Writeline("SILENT");
}       

Now, the problem I have is that since the compiler does not find any code actually changing the values of the SignalOptions class, it simply inlines the values where they are used in the code. I have circumvent the issue by requiring that all ‘option’ fields in the class be volatile, so no optimization is applied, and it works fine, but unfortunately the volatile keyword is not valid on a double.

I have spent much time on the net trying to find a workaround, without success. Is there anyway to either prevent optimizations on the fields or otherwise fool the compiler/jitter into thinking they are used at runtime?

I also would like to put as less as possible the onus on the calling application.

Thanks

  • 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-02T09:34:17+00:00Added an answer on June 2, 2026 at 9:34 am

    I have a local copy here with Parse written as the rather opaque:

    public void Parse(string[] args)
    {    // deliberately opaque, not that it would make any difference
        string fieldName = (new string('S', 1) + "ilentX").Substring(0, 6);
        GetType().GetField(fieldName).SetValue(this, true);
    }
    

    It works fine. I do not believe the problem is what you think it is.

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

Sidebar

Related Questions

I have a system built around protobuf.net, the system exposes an abstract class (foo)
I have two case classes that inherit from an abstract base class. I want
I have built an MSI that I would like to deploy, and update frequently.
I have built a simple C#.Net app on a M/C with only .Net FX
I have built an application in C# that I would like to be optimized
I have built a number of asp.net servercontrols into a class library, & I
I have built a .dll under WinXP that claims it can't find DWMAPI.DLL when
I have an abstract class CommandPath, and a number of derived classes as below:
I have a class similar to the following: public abstract class Manager<T, TInterface> :
I've written an abstract base class for unit tests that sets up just enough

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.