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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:33:05+00:00 2026-05-25T16:33:05+00:00

I am writing a ConfigParser class, which reads from a config file structured like

  • 0

I am writing a ConfigParser class, which reads from a config file structured like this:

[Section]
option1 = foo
option2 = 12
option3 = ;
...

The information read is actually stored in a Dictionary<string, string>. What i’d like to achieve is the following:

struct ConfigStruct
{
    public string option1;
    public int option2;
    public char option3 { get; set; }
    // Any other _public_ fields or properties
}

ConfigParser Cp = new ConfigParser("path/to/config/file"); // Loads content
ConfigStruct Cs = Cp.CreateInstance<ConfigStruct>("Section");

Console.WriteLine(Cs.option1); // foo
Console.WriteLine(Cs.option2.ToString()); // 12
Console.WriteLine(Cs.option3.ToString()); // ;

The struct (or class, it doesn’t matter) ConfigStruct, is application-specific, and the ConfigParser class should know nothing about it. Basically, I want to parse the value from a specific option, and store it into the field/property with the same name. Parsing should be done according to the field/property type.

I’ve developed a stub method for it:

public T CreateInstance<T>(string Section) where T : new()
{
    // Gets options dictionary from loaded data
    Dictionary<string, string> Options = this.Data[Section];

    T Result = new T();

    Type StructType = Result.GetType();

    foreach (var Field in StructType.GetFields())
    {
        if (!Options.ContainsKey(Field.Name))
            continue;

        Object Value;

        if (Field.FieldType == typeof(bool))
            Value = Boolean.Parse(Options[Field.Name]);

        else if (Field.FieldType == typeof(int))
            Value = Int32.Parse(Options[Field.Name]);

        else if (Field.FieldType == typeof(double))
            Value = Double.Parse(Options[Field.Name]);

        else if (Field.FieldType == typeof(string))
            Value = Options[Field.Name];

        else if (Field.FieldType == typeof(char))
            Value = Options[Field.Name][0];

        // Add any ifs if needed

        else { /* Handle unsupported types */ }

        Field.SetValue(Result, Value);
    }

    foreach (var Property in StructType.GetProperties())
    {
         // Do the same thing with public properties
    }

    return Result;
}
  1. Do you think this is the right approach to the problem? Or should I move the responsability of initializing the struct to the application logic instead of the ConfigParser class? I know it’s more efficient, but using reflection I write this method only once, and works for every struct.
  2. Should I use reflection to invoke Parse() so that I can avoid all those ifs? Or you’d rather make those conversions type by type, to prevent unexpected behaviour?

Thanks for your time.

  • 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-05-25T16:33:05+00:00Added an answer on May 25, 2026 at 4:33 pm

    Assuming there is a specific reason why you are not using app.config/web.config or other built-in configuration files.

    1. I think this comes down to what the rest of the application is doing, but personally I would do it this way. It allows you to get the return type cleanly and you are not passing an extra stuct down the stack that you don’t need to be.

    2. Reflection is a fantastic tool but has some overhead so if the list of types is finite then specifying them manually is more efficient, or alternately only reflecting the unknown types. Also I would change your if blocks to a switch statement, you will gain efficiencies if the IL complier can fully optimise the condition block.

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

Sidebar

Related Questions

Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
I have this python code for opening a .cfg file, writing to it and
Writing a shell script and I want to do something like this: cp myfile.ext
Writing a file utility to strip out all non-ASCII characters from files. I have
Writing a class, how do I implement foo.send(item) ? __iter__ allows iterating over the
Writing a Reporting Service (2005) report My DataSet returns something like this: DESCRIPTION COUNT
Writing a Service that is running on IIS. Basically looks like this: void ProcessRequest(HttpContext
Writing the code below without dot notation, do I have this right? from: if(self.yellowViewController.view.superview
Writing a function I must declare input and output data types like this: int
Writing a client / server program and previously was using calls like this: TcpListener.AcceptTcpClient()

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.