Question

I am writing cscfg file. I want to present one of its values to be enum:

enum Importance
{
    None,
    Trivial,
    Regular,
    Important,
    Critical
};

I cscfg file I have a following Setting:

<Setting name="MySettings" value="None">
  1. Is it a correct way to present enum in cscfg?
  2. How do I read this value to actual enum? And how do I validate if the value doesn`t match enum?

For example:

<Setting name="MySettings" value="Kuku">
Was it helpful?

Solution 2

You can use Enum.TryParse for this:

var value = valueFromConfigFile;
Importance val;
if (Enum.TryParse(value, true, out val)){
    // OK, go ahead
}
else{
    // enum not recognized
}    

OTHER TIPS

Read the value just like you read any other configuration in a string. Then use Enum.TryParse<> to check and convert the string to an enum.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top