如何为单个特定枚举创建扩展方法
我有一个枚举类,其中充满了第三方应用程序的命令行开关。我正在尝试制作它,以便我可以使用 .ToSwitch (或其他)调用枚举中的项目,以便我可以制作它验证值类型,输出格式化的命令行开关及其参数值。也许我的做法是错误的。坐在这里一整天后(它很大),我发现很难放手并探索不同的方法。哈哈有什么建议吗?
期望的结果:
Console.WriteLine(EnumClass.EnumItem.ToSwitch("option"));
会吐出:-x“选项”
I've got an enum class full of command line switches for a third party app..I'm trying to make it so I can call on an item in the enum with a .ToSwitch (or whatever) so that I can make it validate the value type, spit out a formatted command-line switch with its parameter value. maybe I'm going about it wrong. After sitting here all day working on it (it's big) I'm finding it hard to let go and explore a different approach. lol any suggestions?
desired result:
Console.WriteLine(EnumClass.EnumItem.ToSwitch("option"));
would spit out: -x "option"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于特定的枚举:
执行您所讨论的操作的另一种方法是使用
Dictionary
,这可能更可取。键是开关(或枚举值),值是命令格式。比如:我怀疑这比定义枚举更易于维护。
For a specific enum:
Another way to do what you're talking about is with a
Dictionary
, which might be preferable. The keys would be the switch (or the enum value), and value would be the command format. Something like:I suspect that would be more maintainable than defining an enum.