如何在Windows窗体设置文件中存储枚举值?

时间:2021-03-13 19:45:43

I'm using Windows Forms and VS2008. I want to store an enum value in my application's settings file.

我正在使用Windows Forms和VS2008。我想在我的应用程序的设置文件中存储枚举值。

The settings editor in VS2008 only gives me a limited set of types. Amazingly, enums don't seem to be one of these types that are automatically supported - have I understood this correctly?

VS2008中的设置编辑器只为我提供了一组有限的类型。令人惊讶的是,枚举似乎不是自动支持的这些类型之一 - 我是否理解正确?

From reading up on the subject, it seems like I might need to write a TypeConverter class, to enable my enum to converted to string and vice versa. I've implemented this for my enum, and added the TypeConverter property to my enum, to indicate which converter class to use for that enum.

通过阅读这个主题,似乎我可能需要编写一个TypeConverter类,以使我的枚举转换为字符串,反之亦然。我已经为我的枚举实现了这个,并将TypeConverter属性添加到我的枚举中,以指示要用于该枚举的转换器类。

However, when I try to specify this in my settings file (in the 'Select a Type') dialog, it just says that my type is not defined, so I'm kind of stuck.

但是,当我尝试在我的设置文件(在“选择类型”)对话框中指定它时,它只是说我的类型没有定义,所以我有点卡住了。

Can anyone explain to me how I store an enum in a Settings file in a Windows Forms app? It seems like such a crushingly simple (and commonly required) piece of functionality that I'm amazed it's not already supported, and that I seem to have to do so much work to get it to work (and for only one enum!).

任何人都可以向我解释如何在Windows窗体应用程序的设置文件中存储枚举?它看起来像是一个非常简单(通常需要)的功能,我很惊讶它还没有被支持,而且我似乎必须做很多工作才能让它工作(并且只有一个枚举!)。

Therefore I think I must be missing something, and it's actually really easy...

因此,我认为我必须遗漏一些东西,而且实际上很容易......

Let's say my enum looks like this:

让我们说我的枚举看起来像这样:

namespace MyApp
{
    enum MyEnum
    {
        Yes,
        No
    }
}

...how do I store a value from this enum in my settings file? (And retrieve it, of course).

...如何在我的设置文件中存储此枚举的值? (当然,检索它)。

(Obviously I can just store a string or integer and interpret myself, but that seems pretty clunky, and I'd expect Windows Forms to handle this sort of thing more cleanly.)

(显然我可以只存储一个字符串或整数并解释自己,但这看起来很笨重,而且我希望Windows Forms更干净地处理这类事情。)

1 个解决方案

#1


3  

Enums are actually not that far removed from numeric types (default int) and can be used interchangably. I don't think it is clunky to cast back and forth and store the int. In fact only strings can be stored in the settings file. This does mean, by extension, anything that is serializable to string.

枚举实际上并没有远离数字类型(默认为int),并且可以互换使用。我不认为来回转换并存储int是笨重的。实际上只有字符串可以存储在设置文件中。这通过扩展意味着任何可序列化为字符串的东西。

Another way would be to store the text value (so it's human editable) of the enum and parse it using Enum.Parse(type, string).

另一种方法是存储枚举的文本值(因此它是人类可编辑的)并使用Enum.Parse(type,string)对其进行解析。

#1


3  

Enums are actually not that far removed from numeric types (default int) and can be used interchangably. I don't think it is clunky to cast back and forth and store the int. In fact only strings can be stored in the settings file. This does mean, by extension, anything that is serializable to string.

枚举实际上并没有远离数字类型(默认为int),并且可以互换使用。我不认为来回转换并存储int是笨重的。实际上只有字符串可以存储在设置文件中。这通过扩展意味着任何可序列化为字符串的东西。

Another way would be to store the text value (so it's human editable) of the enum and parse it using Enum.Parse(type, string).

另一种方法是存储枚举的文本值(因此它是人类可编辑的)并使用Enum.Parse(type,string)对其进行解析。