views:

41

answers:

2

In Visual Studio 10 (probably other versions too) it's possible to define application settings using a designer view. These settings appear to simply be public variables stored.

Is there any way to use a custom enum as the type of an application setting?

I notice that you can browse the references for enums, but can't seem to find a way to specify an enum contained within my code.

+6  A: 

The short version is:

You can create your own ConfigurationSection and ConfigurationSectionGroup (reference System.Configuration, first). Then, you define a in your web.config to support your custom sections.

If you want intellisense, you need to modify C:\Program Files (x86)\Microsoft Visual Studio 10.0\Xml\Schemas\DotNetConfig.xsd to include the detail of your new sections.

For example, let's say you want config that looks like this:

<tomWright.Framework>
    <settings awesomeness="High" coolness="Medium"/> 
</tomWright.Framework>

You'd create a ConfigurationSectionGroup called something like TomWrightFrameworkConfigurationSectionGroup (which inherits from ConfigurationSectionGroup) and have it have one property called Settings, which points to a another class SettingsConfigurationSection (which inherits from ConfigurationSection). There are a few examples on MSDN on how to do this.

It's a bit of a learning curve, but when you want to make the leap from simple appSettings, to custom configuration - there's really only one way to do it, and it's not particuarly intuitive. If you have any questions though, let me know, I've done a fair with this before. Hope that helps... -Rob

Robert Seder
A: 

IIRC you can store custom data types in Application settings but the type needs to be XML serializable or have a type converter that implements tostring/fromstring methods. For enum types I think you just need to mark it with the SerializableAttribute. Not at my work computer right now to check my memory.

Bear Monkey