English 中文(简体)
Enum. 不能 cast笑
原标题:Enum.Parse fails to cast string

我试图将一些排入物体。 情况如下:

<?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>

The Logging_levels represents several enum values that are allowed by the settings. I m trying to load these into my object by using the following code:

Level = (LogLevel)Enum.Parse(typeof(LogLevel), settings["Logging_levels"]);

但是,这并不奏效,我只想达到后勤水平。 信息回来,而不是后勤水平的价值。 离职偿金。 预警数据。 信息。 表格的定义如下:

[Flags]
public enum LogLevel
{
    Error = 0x0,
    Warning = 0x1,
    Info = 0x2,
    Debug = 0x3,
    Performance = 0x4
}

我错过了,用黑手法界定了价值观? 或者我是否对其他东西有误解?

最佳回答

您的<代码>enum值将引起问题。

<代码>Flags>> 必须有两种权力,除某种空洞/无标志外,任何价值不得使用0。

这是否有什么变化?

[Flags]
public enum LogLevel
{
    None        =  0
    Error       =  1,
    Warning     =  2,
    Info        =  4,
    Debug       =  8,
    Performance = 16
}
问题回答

适当的旗帜价值可能有助于(每种价值应当设定一个不同的轨道):

[Flags]
public enum LogLevel
{
    None = 0x0,
    Error = 0x1,
    Warning = 0x2,
    Info = 0x4,
    Debug = 0x8,
    Performance = 0x10
}

注:如果你愿意,你可以删除。

问题 t! 问题在于改为案文。 由于国旗值是独一无二的比照面罩,因此没有定型绘图以示和背。 正如其他人提到的那样,你应当选择诸如:

[Flags]
public enum LogLevel
{
    None = 0
    Error = 1 << 0,
    Warning = 1 << 1,
    Info = 1 << 2,
    // etc
}

。MSDN: Parsing Enumer Values

See a demo Live: https://ideone.com/8AkSQ

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);        
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))  
               Console.WriteLine("Converted  {0}  to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine(" {0}  is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}

产出:

Converted  0  to None.
Converted  2  to Green.
8 is not an underlying value of the Colors enumeration.
 blue  is not a member of the Colors enumeration.
Converted  Blue  to Blue.
 Yellow  is not a member of the Colors enumeration.
Converted  Red, Green  to Red, Green.

Performance Note:

如果业绩很重要,那么不是依靠Enum.Parse :





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签