开发者

Custom enum as application setting type in C#?

开发者 https://www.devze.com 2022-12-18 15:42 出处:网络
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

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开发者_C百科 Enum, but in the Value (where Windows, Macintosh should be) only Windows is visible and i'm able to enter any string.


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.


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.


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.


I know this post is very old and has been marked as answered. But maybe I can save some time for people out there that tries the solution(s) mentioned above.

I am using VS2019 and as shown by AlanN and Sky Sanders, one is able to define the custom type, by entering either the full name manually, or by picking it from the list. The latter only works, when it was defined in another referenced assembly (don't forget to build it beforehand), which is a known limitation as explained here: DocMicrosoft: Create Application Settings using the designer.

My whole point of doing it that way was to give the user fixed options so that he does not have to check which strings are allowed and which are not. The problem is that the options do not consistently show up. Sometimes, after compiling your programm, you will not see the options anymore; they are gone. Sometimes, after restarting Visual Studio, they are back again. This problem was already stated by Sean in another stackoverflow post here.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号