简体   繁体   English

C#:枚举值可以保存为设置吗?

[英]C#: Can an Enum Value be saved as a Setting?

Can an enum value be saved as a setting, using the Properties.Settings.Default["MySetting"] syntax of C#? 可以使用C#的Properties.Settings.Default["MySetting"]语法将enum值保存为设置吗? I tried to create a setting in my project's property pages, but only system classes appeared in the list of available types. 我尝试在项目的属性页面中创建一个设置,但只有系统类出现在可用类型列表中。

If it can be done, how do I do it? 如果可以,我该怎么办? Thanks in advance for your help. 在此先感谢您的帮助。

just store it as an int and convert it when needed. 只需将其存储为int并在需要时将其转换。

Properties.Settings.Default["MySetting"] = myEnumValue;

// and later 
var settingValue = Properties.Settings.Default["MySetting"];
MyEnum value = (MyEnum)settingValue;

If you feel the need you can use Enum.IsDefined(typeof(MyEnum), value) to make sure it is valid. 如果您觉得有必要,可以使用Enum.IsDefined(typeof(MyEnum), value)来确保它有效。 You can also store a string value so that it is in a human-readable format in your config file: 您还可以存储字符串值,以便在配置文件中使用人类可读的格式:

Properties.Settings.Default["MySetting"] = myEnumValue.ToString();

// and later 
var settingValue = Properties.Settings.Default["MySetting"];
MyEnum value = (MyEnum)Enum.Parse( typeof(MyEnum), settingValue );

It is an old post but I think this solution is worth publishing for whom may encounter the same issue. 这是一个老帖子,但我认为这个解决方案值得发布,可能会遇到同样的问题。
Basically it consists in creating a new library to be referenced by the main project so that this library exposes the enum as a new type that can be selected from Properties.Settings.settings . 基本上它包括创建一个由主项目引用的新库,以便该库将枚举公开为可以从Properties.Settings.settings中选择的新类型。 In my case I want to enumerate levels of severity. 在我的情况下,我想列举严重程度。

The new Library 新图书馆
Under your current solution, create a new empty Class Library with the code below: 在您当前的解决方案下,使用以下代码创建一个新的空类库:

namespace CustomTypes
{
    [Serializable]
    public enum Severity
    {
        INFO,
        WARNING,
        EXCEPTION,
        CRITICAL,
        NONE
    }
}

Reference the Library 参考图书馆

  • Compile and reference the newly created library in all the projects that are going to use this type. 在将要使用此类型的所有项目中编译并引用新创建的库。
  • Now open your project's Properties => Settings. 现在打开项目的Properties => Settings。
    The new library may not be yet visible in the type DropDown list. 在DropDown type列表中可能尚未显示新库。 If you don't see it, select Browse at the bottom of the DropDown and try to find the library. 如果您没有看到它,请选择DropDown底部的Browse并尝试查找库。
    If it is still not visible, type the full path to the new type in the Selected Type field. 如果仍然不可见,请在“ Selected Type字段中Selected Type新类型的完整路径。 (In this example, enter "CustomTypes.Severity" as shown below: (在此示例中,输入“CustomTypes.Severity”,如下所示:

    在此输入图像描述

    From now on, the new type should be visible and usable in Properties.Settings.settings. 从现在开始,新类型应该在Properties.Settings.settings中可见并可用

    在此输入图像描述

Here is how I set the Setting type to MyEnum enum type: 以下是我将Setting类型设置为MyEnum枚举类型的方法:

  1. Create your new property setting of type string (type string will be changed to MyEnum type in the next steps) 创建string类型的新属性设置(在接下来的步骤中,类型string将更改为MyEnum类型)

  2. Open Settings.Designer.cs 打开Settings.Designer.cs

: 在此输入图像描述

  1. Modify the return type of your new Property from string to your enum type MyEnum : 将新属性的返回类型从string修改为枚举类型MyEnum

    Given the Setting Enum Type is named MyEnum (in Company.Enums namespace), edit the return type to be of type global::Company.Enums.MyEnum : 如果设置枚举类型名为MyEnum (在Company.Enums命名空间中),请将返回类型编辑为global::Company.Enums.MyEnum类型:

      [global::System.Configuration.ApplicationScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("MySetting")] public global::Company.Enums.MyEnum MyEnum{ get { return ((global::Company.Enums.MyEnum)(this["MyEnum"])); } } 

This way it looks much cleaner, with only one statement; 这样看起来更干净,只有一个陈述; type safety is ensured on the client side, without the need to do the unnecessary string to enum parsing. 在客户端确保类型安全,而不需要执行不必要的字符串来枚举解析。

MyEnum value = Properties.Settings.Default["MySetting"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM