自定义枚举作为 C# 中的应用程序设置类型?

发布于 2024-08-19 23:31:54 字数 306 浏览 4 评论 0 原文

如果 C# 中有一个枚举:

[Serializable]
public enum OperatingSystem 
{
    Windows,
    Macintosh
}

对于我的应用程序,我使用应用程序设置,我可以在其中选择设置的类型。我认为当我选择“浏览”时,我可以选择我的枚举或键入完全限定的路径以选择该枚举作为类型。

编辑:
我将类型设置为枚举,但在值(Windows、Macintosh 应该是)中,只有 Windows 可见,并且我可以输入任何字符串。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

追风人 2024-08-26 23:31:54

我在 VC# Express 2005 中看到了这一点。浏览..“选择类型”对话框仅显示系统和 Microsoft 命名空间。但是,如果您将类型的全名插入“选定类型”文本框中,它应该接受它。

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.

影子是时光的心 2024-08-26 23:31:54

对之前所有答案的小补充。
对于我来说 - 在我的自定义枚举成功添加到“自定义类型”文本框中之前,需要构建解决方案。

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.

花伊自在美 2024-08-26 23:31:54

当然 - 只需将可序列化的枚举添加到您的项目中,选择浏览并输入命名空间限定名称,例如 ClassLibrary1.OperatingSystems。宾果游戏。

“我将类型设置为我的枚举,但在
值(Windows、Macintosh 应
是)只有 Windows 是可见的,我是
可以输入任何字符串”。

您是否尝试过输入“windows”或“macintosh”以外的其他内容?您看到的行为与您想要的非常接近。我对此非常有信心。

无论如何...祝你好运。

此处 30 秒内完成

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.

江湖彼岸 2024-08-26 23:31:54

我知道这篇文章很旧并且已被标记为已答复。但也许我可以为那些尝试上述解决方案的人节省一些时间。

我正在使用 VS2019,如 AlanNSky Sanders 所示,可以通过手动输入全名或从列表。后者只有在另一个引用的程序集中定义时才有效(不要忘记事先构建它),这是一个已知的限制,如下所述:DocMicrosoft:使用设计器创建应用程序设置

我这样做的全部目的是为用户提供固定的选项,这样他就不必检查哪些字符串是允许的,哪些是不允许的。问题是选项并不一致显示。有时,编译程序后,您将不再看到这些选项;他们走了。有时,重新启动 Visual Studio 后,它们会再次出现。 Sean 在另一篇 stackoverflow 帖子 此处

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.

随波逐流 2024-08-26 23:31:54

我不知道我是否很好地理解了你的问题,但是当我创建 自定义配置部分在我的应用程序配置文件中使用我使用 TypeConverter 属性 以及 EnumConverter

我希望它有帮助,但如果我错误地理解了你的问题,请告诉我。

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.

停滞 2024-08-26 23:31:54

不要忘记添加对项目的引用,因为枚举不应(!)位于您的主项目中,它应该(!)位于其他地方。
然后,在主项目属性的设置选项卡中显示“字符串”的地方,您可以通过单击下拉框来选择另一个选项。从选项中选择“浏览”。您将看到一些通用的 Microsoft 选项,但请键入以其命名空间为前缀的枚举名称,例如 Common.MyEnum。
那应该可以解决问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文