Custom enum as application setting type in C#?

南楼画角 提交于 2019-12-29 03:28:32

问题


If have an enum in C#:

[Serializable]
public enum OperatingSystem 
{
    Windows,
    Macintosh
}

For my application I use the application settings, where I can select of which Type a setting should be. I thought when I select Browse, I could choose my enum or type the fully qualified path to select that enum as the Type.

Edit:
I set the type to my Enum, but in the Value (where Windows, Macintosh should be) only Windows is visible and i'm able to enter any string.


回答1:


I see this in VC# Express 2005. The Browse.. "Select a Type" dialog shows only the System and Microsoft namespaces. However if you insert the full name of the type into the Selected Type textbox it should accept it.




回答2:


Small addition to all previous answers. As for me - it was needed to BUILD solution before my custom enum has been successfully added to the "Custom type" text box.




回答3:


Sure - just add a serializable enum to your project, select browse and type in the namespace qualified name, e.g. ClassLibrary1.OperatingSystems. Bingo.

"I set the type to my Enum, but in the Value (where Windows, Macintosh should be) only Windows is visible and i'm able to enter any string".

Have you tried entering something other than 'windows' or 'macintosh'? The behavior you see is as close to what you want as you are going to get. I am pretty confident on that.

In any case... good luck.

See it done in 30 seconds here.




回答4:


I don't know if I have understood your question very well, but when I create a custom configuration section to use at my application configuration files I create a enum property using a TypeConverter attribute together with a EnumConverter.

I hope it helps, but let me know if I understood your question incorrectly.




回答5:


Don't forget to add a reference to the project as the enum shall (!) not be in you main-project, it should be (!) somewhere else. Then where it says "string" in your settings-tab of the main-projects' properties, you can choose another option by clicking the drop-down box. Select "Browse" from the options. You will see some generic Microsoft options, but type your enum-name prefixed by its namespace such as Common.MyEnum. That should do the trick.



来源:https://stackoverflow.com/questions/2184209/custom-enum-as-application-setting-type-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!