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">
For example:
<Setting name="MySettings" value="Kuku">
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.
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
}
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.