23

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!

Elad Lachmi
  • 10,406
  • 13
  • 71
  • 133
  • check these http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/5a700783-0425-43a4-bdc3-0f85a029d3f9 http://stackoverflow.com/questions/478403/can-i-add-and-remove-elements-of-enumeration-at-runtime-in-java – Niranjan Singh Nov 16 '11 at 08:27
  • @NiranjanKala - So is there an alternative? I just want type safety for a type unknown in compile-time. Is this even possible? – Elad Lachmi Nov 16 '11 at 08:32
  • Do you have a strongly-typed enum declared in your code? Or you want to do everything dynamic? – Cheng Chen Nov 16 '11 at 08:35
  • You can look into [T4 Text Templates](http://msdn.microsoft.com/en-us/library/bb126445.aspx). I didn't used it before, but yesterday on [this thread](http://stackoverflow.com/questions/8133731/can-i-get-enum-like-functionality-for-strings-in-net/8133783#8133783) I heard about it. – NaveenBhat Nov 16 '11 at 08:40
  • //convert string to enum FileTypes c = (FileTypes) Enum.Parse(typeof(FileTypes), "MOV", true); you can use this to convert your string value of your EnumBuilder class inherited enum type.. http://msdn.microsoft.com/it-it/library/system.reflection.emit.enumbuilder%28VS.80%29.aspx – Niranjan Singh Nov 16 '11 at 08:42
  • If the values are going to change often enough to need to load them out of a config file rather then out of source code, then an `enum` might not be the right tool for the job. – Merlyn Morgan-Graham Nov 16 '11 at 08:54

3 Answers3

32

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); 
Ondrej
  • 1,209
  • 1
  • 11
  • 21
David Horák
  • 5,535
  • 10
  • 53
  • 77
0

Microsoft.Extensions.Configuration will deserialse enums stored in strings in JSON, which I find a great way to solve this problem.

As an example:

public enum Format
{
    UNKNOWN = 0,
    PNG,
    JPEG
}

public class ImageOptions
{
    /* List of supported file types */
    public List<Format> SupportedFileTypes { get; set; }
}
/* Options config dict */
public static Dictionary<string, string> DefaultImageServiceConfigDict = new Dictionary<string, string>
{
    /* Image Options NOTE: Enums stored as strings!! */
    {"ImageServiceOptions:SupportedFileTypes:0", "png"},
    {"ImageServiceOptions:SupportedFileTypes:1", "jpeg"},
};

/* Read options */
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(DefaultImageServiceConfigDict);
configuration = builder.Build();

/* Parses config, including enum deserialisation by name */
imageConfig = config.GetSection(nameof(ImageOptions)).TryGet<ImageServiceOptions();
James
  • 30,496
  • 19
  • 86
  • 113
0

If I were you then I would design my own Enum class. That way you'll either be able to serialize it into XML or build it at runtime. It'll also ensure that you still have type safety.

Typically the data will be stored either in a dictionary type or key/value pair list within the class. Then you can store a list of values in the config file (have a look at how to read List data)

Have a look here in order to get some ideas.

ChrisBD
  • 9,104
  • 3
  • 22
  • 35