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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:03:46+00:00 2026-05-28T04:03:46+00:00

I have a custom configuration section registered in the app/web.config, let’s call it MySection

  • 0

I have a custom configuration section registered in the app/web.config, let’s call it MySection. I have an ElementCollection element inside the section, called MyElements. Inside the element collection I want to have elements which are represented by different classes – the idea is that these are similar classes with some common properties and some specific to the instance.

Here is some xml configuration example:

<MySection>
  <MyElements>
    <Element1 name="someProp1" value="someValue" />
    <Element2 name="someProp2" format="{0}{1}" />
  </MyElements>
</MySection>

In my simple example, all elements must have a ‘name’ property, some will have also a ‘value’ property, and the other a ‘format’ property.
Here, I want Element1 and Element2 to be represented in the .NET runtime by two different classes which have a common base class that defines the ‘name’ property.

As far as I have dug into .NET configuration, I got the impression that an element collection (like ‘MyElements’ here) should contain homogeneous elements (only of one type). So, could it be possible to achieve what I want – make it contain elements of different classes. The idea is to avoid both having more than one element collection for different element types and not to write all repeating properties for every custom ConfigurationElement implementation.

  • 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-28T04:03:46+00:00Added an answer on May 28, 2026 at 4:03 am

    You can achieve this by overriding OnDeserializeUnrecognizedElement method in your ElementCollection class and creating representations of your Element1 and Element2 by switching on tag name for ex. But AFAIR child elements should be derived from common ancestor anyway, doing it otherwise is too much trouble.

    Define collection as:

    public class MyElementCollection : ConfigurationElementCollection
    {
        const string ELEMENT1 = "Element1";
        const string ELEMENT2 = "Element2";
    
        protected override ConfigurationElement CreateNewElement ()
        {
            return new MyElement (this);
        }
    
        protected override object GetElementKey (ConfigurationElement element)
        {
            return ((MyElement)element).Key;
        }
    
        // This method called when framework sees unknown element tag
        // inside the collection. You can choose to handle it yourself
        // and return true, or return false to invoke default action
        // (exception will be thrown).
        protected override bool OnDeserializeUnrecognizedElement (string elementName, XmlReader reader)
        {
            if (elementName == ELEMENT1 || elementName == ELEMENT2 {
                var myElement = new MyElement (this);
    
                switch (elementName) {
                case ELEMENT1:
                    myElement.Type = MyElementType.Element1;
                    break;
                case ELEMENT2:
                    myElement.Type = MyElementType.Element2;
                    break;
                }
    
                myElement.DeserializeElementForConfig (reader, false);
                BaseAdd (myElement);
    
                return true;
            }
    
            return false;
        }
    }
    

    And child element:

    public enum MyElementType
    {
        Element1,
        Element2,
    }
    
    public class MyElement : ConfigurationElement
    {
        const string NAME = "name";
        const string VALUE = "value";
        const string FORMAT = "format";
    
        // keys should be unique, current collection count will do
        // the trick without introducing artificial keys
        public MyElement (ConfigurationElementCollection collection)
        {
            Key = collection.Count;
        }
    
        // note that this is not ConfigurationProperty
        public int Key { get; private set; }
    
        // note that this is not ConfigurationProperty
        public MyElementType Type { get; set; }
    
        [ConfigurationProperty(NAME)]
        public string Name {
            get { return (string)this [NAME]; }
        }
    
        [ConfigurationProperty(VALUE)]
        public string Value {
            get { return (string)this [VALUE]; }
        }
    
        [ConfigurationProperty(FORMAT)]
        public string Format {
            get { return (string)this [FORMAT]; }
        }
    
        // This is called when framework needs a copy of the element,
        // but it knows only about properties tagged with ConfigurationProperty.
        // We override this to copy our Key and Type, otherwise they will
        // have default values.
        protected override void Reset (ConfigurationElement parentElement)
        {
            base.Reset (parentElement);
    
            var myElement = (MyElement)parentElement;
            Key = myElement.Key;
            Type = myElement.Type;
        }
    
        // original ConfigurationElement have this protected,
        // redeclaring as protected internal to call it from collection class
        protected internal void DeserializeElementForConfig (XmlReader reader, bool serializeCollectionKey)
        {
            DeserializeElement (reader, serializeCollectionKey);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom configuration section within web.config file. I'm lingering between: Reading it into
I have defined a custom section in the App.config file and all the Configuration
I have a web application with a custom configuration section. That section contains information
I've create a custom configuration section in my web.config file for my 3.5 web
We have a custom section in my app.config file related to our IoC container
I have custom ConfigurationSection and call Configuration.Save() after some modifications against it: var config
I have a windows service that has a custom configuration section. In the configSectionHandler
I have custom errors configured in my web.config, but IIS 6.0 is returning the
I have just rolled a custom configuration section, created an accompanying schema document for
I have in the app.config file a section as <OurType> <sometype typename = type1

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.