Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get enum values from web.config at run time

Tags:

I have an enum which I want to get from the web.config at run-time. I started reading about build providers, but this seems to work for classes. Can someone point me to an example, or at least point me in the right direction.

Right now I have a comma separated list of values in the web.config, and this is not type-safe and is prone to errors.

If there is another approach to get this type of "dynamic-enum", I'm open to other ideas.

Thank you!

like image 301
Elad Lachmi Avatar asked Nov 16 '11 08:11

Elad Lachmi


1 Answers

You can use ConfigurationManager and convert value to enum:

<configuration>    <appSettings>     <add key="YourEnum" value="BlueSky" />     </appSettings> </configuration> 
string configValue = ConfigurationManager.AppSettings["YourEnum"]; YourEnumType value = (YourEnumType)Enum.Parse(typeof(YourEnumType), configValue);  
like image 146
David Horák Avatar answered Dec 06 '22 18:12

David Horák