rohan singh

software engineer. bicyclist & rock climber. craft beer addict.

ConfigurationProperty Code Snippet (C#)

If you’ve ever wanted to create a custom configuration section for a .NET app, you know how tedious it is to type this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace Samples.AspNet
{
    public class PageAppearanceSection : ConfigurationSection
    {
        // Create a "remoteOnly" attribute.
        [ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
        public Boolean RemoteOnly
        {
            get
            {
                return (Boolean)this["remoteOnly"];
            }
            set
            {
                this["remoteOnly"] = value;
            }
        }

        // Create a "font" element.
        [ConfigurationProperty("font")]
        public FontElement Font
        {
            get
            {
                return (FontElement)this["font"]; }
            set
            { this["font"] = value; }
        }

...

There’s a much faster way to create normal properties. If you’ve ever used the prop code snippet to create a property, you already know this. You can just type “prop” and hit tab to have an entir property filled out for you:

Faced with the daunting task of creating some custom configuration elements, I threw together a code snippet that lets you do the same for a configuration-backed property. I can now just type “propc” and hit tab to get this:

Then I just fill out the name and type and call it good. If you’d like to join in on the propc goodness here’s the source of the snippet file. Just download and save it as a .snippet file and import it through Tools | Code Snippets Manager in Visual Studio:

Comments