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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:23:05+00:00 2026-05-30T07:23:05+00:00

I’m trying to maintain a dictionary of configurations. Here is my abstract class. [Serializable]

  • 0

I’m trying to maintain a dictionary of configurations.

Here is my abstract class.

[Serializable]
public abstract class Configuration
{
}

And here is a concrete class (for the moment, I just have only this class).

[Serializable]
public class BinaryProblemConfiguration : Configuration
{
    [XmlAttribute]
    public decimal MinValue { get; set; }
    [XmlAttribute]
    public decimal MaxValue { get; set; }
}

I’ve got a class which contains a Dictionary of configuration levels.

  • The first parameter is the name of the configuration. When name="" means default configuration.
  • Level means the difficulty. There are three levels: Easy, Medium and Hard.
  • And the third one is the configuration.
/// <summary>
/// The abstract level configuration allows descendent classes to configure themselves
/// </summary>
public abstract class LevelConfiguration
{
    private Dictionary<string, Dictionary<Levels, Configuration>> _configurableLevels = new Dictionary<string, Dictionary<Levels, Configuration>>();

    /// <summary>
    /// Adds a configurable level.
    /// </summary>
    /// <param name="level">The level to add.</param>
    /// <param name="problemConfiguration">The problem configuration.</param>
    protected void AddConfigurableLevel(string name, Levels level, Configuration problemConfiguration)
    {
        if (!_configurableLevels.ContainsKey(name))
        {
            _configurableLevels.Add(name, new Dictionary<Levels, Configuration>());
        }

        _configurableLevels[name].Add(level, problemConfiguration);
    }

    /// <summary>
    /// Returns all the configurable levels.
    /// </summary>
    /// <param name="level"></param>
    protected void RemoveConfigurableLevel(string name, Levels level)
    {
        _configurableLevels[name].Remove(level);
    }

    /// <summary>
    /// Returns all the configurable names.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<string> GetConfigurationNames()
    {
        return _configurableLevels.Keys;
    }

    /// <summary>
    /// Returns all the configurable levels.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<Levels> GetConfigurationLevels(string name)
    {
        return _configurableLevels[name].Keys;
    }

    /// <summary>
    /// Gets the problem configuration for the specified level
    /// </summary>
    /// <param name="level">The level.</param>
    /// <returns></returns>
    public Configuration GetProblemConfiguration(string name, Levels level)
    {
        return _configurableLevels[name][level];
    }
}

This is the class which create some configurations. I’m creating three default configs and two customs.

public class AdditionLevelConfiguration : LevelConfiguration
{
    public AdditionLevelConfiguration()
    {
        AddConfigurableLevel("", Levels.Easy, GetEasyLevelConfiguration());
        AddConfigurableLevel("", Levels.Medium, GetMediumLevelConfiguration());
        AddConfigurableLevel("", Levels.Hard, GetHardLevelConfiguration());

        AddConfigurableLevel("config2", Levels.Easy, GetEasyLevelConfiguration());
        AddConfigurableLevel("config2", Levels.Medium, GetMediumLevelConfiguration());

        var configs = this.GetProblemConfiguration("config2", Levels.Medium);
        var configs2 = this.GetProblemConfiguration("", Levels.Easy);
    }

    protected Configuration GetHardLevelConfiguration()
    {
        return new BinaryProblemConfiguration
        {
            MinValue = 100,
            MaxValue = 1000,
        };
    }

    protected Configuration GetMediumLevelConfiguration()
    {
        return new BinaryProblemConfiguration
        {
            MinValue = 10,
            MaxValue = 100,
        };
    }

    protected Configuration GetEasyLevelConfiguration()
    {
        return new BinaryProblemConfiguration
        {
            MinValue = 1,
            MaxValue = 10,
        };
    }
}

I plan to write these configurations in a XML file. I was thinking of serialize them, but it throws me an error. What should I do?

  • 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-30T07:23:06+00:00Added an answer on May 30, 2026 at 7:23 am

    Another option you may consider is using a DataContractSerializer. I was able to serialize your dictionary of dictionaries this way.

    Things to keep in mind if you go this route:

    • You would have to add different attributes. Specifically, you need DataContract on the type and DataMember on the properties.
    • You need to ensure all properties have setters, even if the setters ultimately are private.
    • When creating your DataContractSerializer, you need to ensure it is aware of all your types. Giving it the type of your dictionary takes care of string, Levels, and Configuration, but not BinaryProblemConfiguration. Additional type info can be provided via additional constructor overloads.

    Example:

    var  dict = new Dictionary<string,Dictionary<Levels,Configuration>> ();
    var  wtr = XmlWriter.Create (Console.Out);
    var  dcSerializer = new DataContractSerializer (dict.GetType (), new [] {typeof (BinaryProblemConfiguration)});
    dcSerializer.WriteObject (wtr, dict);
    

    If you do the above, you could also switch to DataContractJsonSerializer later for a more compact JSON format if you prefer (assuming XML is not a hard requirement, of course).

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

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.