有没有办法将一个枚举映射到 C#.NET 3.5 中的另一个枚举?
我正在尝试设置一个枚举,将某些特定于项目的值映射到标准 System.Drawing.Color 枚举。
这是我想做的事情的想法:
public enum SessionColors
{
Highlights = Color.HotPink,
Overlays = Color.LightBlue,
Redaction = Color.Black
}
目标是拥有它,以便我可以将 SessionColors.Highlights 用于我识别为突出显示的内容,但是,我可以稍后更改枚举映射并影响所有后续颜色。
我意识到我可以查找 Color.HotPink 等的值,然后使用这些值,但不太清楚。 还有更好的主意吗?
I'm trying to set up an enumeration that maps certain project-specific values to the standard System.Drawing.Color enum.
Here's the idea of what I'd like to do:
public enum SessionColors
{
Highlights = Color.HotPink,
Overlays = Color.LightBlue,
Redaction = Color.Black
}
The goal is to have it so I can use SessionColors.Highlights for things I identify as a highlight and yet, I can change the enumeration mapping later and affect all subsequent colors.
I realize I can look up the values of Color.HotPink, etc.. and just use those but it's not as clear. Any better idea out there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用公共常量即可:
Just do it with public constants:
Colors.HotPink 不是枚举值,它是返回 Color 值的静态类 Colors 的静态属性。 该 Color 值是一个结构体,而不是整数。
因此,您不能使用 Color 作为枚举的基础值,因为这仅限于整数类型。
Colors.HotPink is not an enum value, it's a static property of the static class Colors that returns a Color value. And that Color value is a struct, not an integer.
So you can't use a Color as underlying value of an enum, as that is restricted to the integral types.
我个人会使用静态类中的颜色属性而不是枚举来完成此操作。 这样做有很多优点,但可能最有益的是,这可以让您在运行时从 app.config(或其他一些配置源)加载颜色,而无需强制重新编译。
I would personally do it with Color properties in a static class instead of Enumerations. There are many advantages to this, but possibly the most beneficial would be that this could allow you to load the colors from app.config (or some other configuration source) at runtime, without forcing a recompilation.