cod*_*nny 5 .net c# enums parsing
我正在尝试将一些AppSettings加载到一个对象中.设置如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Logging_default_path" value="C:\Temp" />
<add key="Logging_max_file_size" value="10485760" />
<add key="Logging_levels" value="Error,Warning,Info"/>
<add key="Logging_filename" value="RobinsonLog" />
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
该Logging_levels 表示由设置允许几个枚举值.我正在尝试使用以下代码将这些加载到我的对象中:
Level = (LogLevel)Enum.Parse(typeof(LogLevel), settings["Logging_levels"]);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,我只返回LogLevel.Info,而不是Loglevel.Error |的值 LogLevel.Warning | LogLevel.Info.枚举定义如下:
[Flags]
public enum LogLevel
{
Error = 0x0,
Warning = 0x1,
Info = 0x2,
Debug = 0x3,
Performance = 0x4
}
Run Code Online (Sandbox Code Playgroud)
通过以十六进制定义值,我错了吗?还是我错过了别的什么?
你的enum价值观会引起问题。
a 的值Flags enum必须是 2 的幂,并且除了某种空/无/无指示符之外,不应使用 0 表示任何值。
这有什么区别吗?
[Flags]
public enum LogLevel
{
None = 0
Error = 1,
Warning = 2,
Info = 4,
Debug = 8,
Performance = 16
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1834 次 |
| 最近记录: |