Adding a custom config section

by Robert Mills 26. November 2011 19:10

This is a note to my future self as I had to sort this out this time around. If it helps you, great, else, please ignore it.
The app.config:
<configSections>
    <section name="TexturePacks" type="BlogbyBob.SpaceInvaders.ConfigurationSections.TexturePackConfigurationSection, ConfigurationSections" requirePermission="false"/>
  </configSections>
  <TexturePacks>
    <texturePacks>
      <add name="Classic" playingFieldTexture="Background" scoreBoardTexture="Scoreboard" bunkerTexture="Bunker" blimpTexture="Blimp" playerTexture="Player" playerBoundryTexture="Boundry" missileTexture="Missile" invaderTextureRow1="InvaderRow1" invaderTextureRow2="InvaderRow2" invaderTextureRow3="InvaderRow3" invaderTextureRow4="InvaderRow4" invaderTextureRow5="InvaderRow5" invaderTextureRow6="InvaderRow6" />
    </texturePacks>
  </TexturePacks>
The supporting code:
public class TexturePackElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsKey=true, IsRequired=true)]
        public string Name { get { return (string)this["name"]; } }

        [ConfigurationProperty("playingFieldTexture", IsKey=false, IsRequired=true)]
        public string PlayingFieldTexture { get { return (string)this["playingFieldTexture"]; } }

        [ConfigurationProperty("scoreBoardTexture", IsKey=false, IsRequired=true)]
        public string ScoreBoardTexture { get { return (string)this["scoreBoardTexture"]; } }

        [ConfigurationProperty("bunkerTexture", IsKey=false, IsRequired=true)]
        public string BunkerTexture { get { return (string)this["bunkerTexture"]; } }

        [ConfigurationProperty("blimpTexture", IsKey=false, IsRequired=true)]
        public string BlimpTexture { get { return (string)this["blimpTexture"]; } }

        [ConfigurationProperty("playerTexture", IsKey = false, IsRequired = true)]
        public string PlayerTexture { get { return (string)this["playerTexture"]; } }

        [ConfigurationProperty("playerBoundryTexture", IsKey = false, IsRequired = true)]
        public string PlayerBoundryTexture { get { return (string)this["playerBoundryTexture"]; } }

        [ConfigurationProperty("missileTexture", IsKey = false, IsRequired = true)]
        public string MissileTexture { get { return (string)this["missileTexture"]; } }

        [ConfigurationProperty("invaderTextureRow1", IsKey=false, IsRequired=true)]
        public string InvaderTextureRow1 { get { return (string)this["invaderTextureRow1"]; } }

        [ConfigurationProperty("invaderTextureRow2", IsKey=false, IsRequired=false)]
        public string InvaderTextureRow2 { get { return (string)this["invaderTextureRow2"]; } }

        [ConfigurationProperty("invaderTextureRow3", IsKey=false, IsRequired=false)]
        public string InvaderTextureRow3 { get { return (string)this["invaderTextureRow3"]; } }

        [ConfigurationProperty("invaderTextureRow4", IsKey=false, IsRequired=false)]
        public string InvaderTextureRow4 { get { return (string)this["invaderTextureRow4"]; } }

        [ConfigurationProperty("invaderTextureRow5", IsKey=false, IsRequired=false)]
        public string InvaderTextureRow5 { get { return (string)this["invaderTextureRow5"]; } }

        [ConfigurationProperty("invaderTextureRow6", IsKey=false, IsRequired=false)]
        public string InvaderTextureRow6 { get { return (string)this["invaderTextureRow6"]; } }
    }

    public class TexturePackElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
 	        return new TexturePackElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TexturePackElement)element).Name;
        }

        public TexturePackElement this[int index]
        {
            get { return (TexturePackElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }
    }

    public class TexturePackConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("texturePacks")]
        public TexturePackElementCollection TexturePacks
        {
            get { return (TexturePackElementCollection)this["texturePacks"]; }
        }
    }

The client code (kind of, chopped out what I don't need for this example.

            TexturePackConfigurationSection section = (TexturePackConfigurationSection)ConfigurationManager.GetSection("TexturePacks");

            for (int t = 0; t < section.TexturePacks.Count; t ++)

            {

section.TexturePacks[t]);

            }

Tags:

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

My name is Robert Mills and I am a .NET developer in Charlotte(ish), NC. While everyone is welcome to peruse, most of these posts won't actually provide any value, they are just a reminder to me so I won't forget.

Tag cloud

Month List