简体   繁体   中英

How are enums represented in a web.config file?

I thought this questions was trivial and that Enums were represented simply with an integer but I was surprised that it wasn't the case ! Here's what I have :

I have a custom config file that contains the following TraceEventType Property

  [ConfigurationProperty("Severity")]
  public TraceEventType Severity
  {
     get { return (TraceEventType)this["Severity"]; }
     set { this["Severity"] = value; }
  }

In my config file, I represented this property with the following value :

  ...
  <FileLog  Filename="Test" 
            Severity="1" />

The result : I got this :

System.Configuration.ConfigurationErrorsException: The value of the property 'Severity' cannot be parsed. The error is: The enumeration value must be one of the following: Critical, Error, Warning, Information, Verbose, Start, Stop, Suspend, Resume, Transfer.

You should be able to just use the names of your enum values in the config file, just like the error message states. For example:

<FileLog Filename="Test" Severity="Verbose" />

The System.Configuration classes will take care of parsing the enum for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM