除了 enum 之外,c# 还可以使用什么

发布于 2024-09-16 23:11:12 字数 164 浏览 15 评论 0原文

因此,目前有一个用于应用程序状态的枚举。然而,在用户界面上使用它时,感觉有些不对劲。填充下拉列表时整数和字符串之间的许多转换。我可以使用扩展方法或类型转换器,并继续使用枚举,如果枚举中有多个单词,这将很有帮助。

我想在我把它挖得很深之前,我想先看看是否可以填补一个可能的洞。

谢谢。

So currently have an enumeration used on the status of an application. However, something feels off when using it against the ui. To many conversions between integer and string when populating drop downs. I could use an extension method or the type converter and continue to use the enum which will be helpful if an enum has multiple words in it.

Thought I'd ask to see about filling in a possible hole before I dig it to deep.

Thanks.

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

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

发布评论

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

评论(3

雪化雨蝶 2024-09-23 23:11:12

我的团队在最近的项目中遇到了这个问题。我们保留了枚举,​​因为它们是用于已知常量值的有限列表的东西,但我们做了一些事情以使它们对开发人员更加友好:

  1. 我们用包含“的 [Description()] 属性装饰枚举值每个枚举常量的友好名称”。
  2. 我们创建了一个 GetDescription() 扩展方法,它将反思性地检查枚举常量的 Description 属性并返回友好名称。如果没有,该方法会尝试在枚举常量名称的 ToString() 中插入空格。由于 CamelCased 枚举常量是 ReSharper 强制执行的样式要求,因此这适用于大约 90% 的常量,而 Description 属性则处理其余部分(主要是名称中的大写缩写)。
  3. 我们为字符串创建了一个 ParseEnum() 通用扩展方法,它基本上包装了 Enum.Parse() (这很糟糕;需要在方法中指定 Enum 类型,然后转换为它)。我认为首先尝试查找描述属性可能是足够聪明的,否则我们只需使用仍然可读的 ToString() 表示形式作为下拉项的数据值。

因此,考虑到以下情况:

public enum MyItems
{
   [Description("Item One")]
   ItemOne,
   [Description("Item Two")]
   ItemTwo,
   [Description("Item Three")]
   ItemThree
}

我们可以在两行中用用户友好的选择填充 DropDownList:

foreach(MyValues value in Enum.GetValues<MyValues>())
   myDDL.Items.Add(New ListItem(value.GetDescription(), value.ToString())

...然后我们可以使用非常可读的代码解析选择:

var theValue = myDDL.SelectedItem.Value.ParseEnum<MyValues>()

编辑:我被要求提供 GetDescription() 方法。我对分享整个事情有点犹豫,但这是获取装饰枚举常量的描述属性的基本算法。解析 CamelCased 名称是非常简单的正则表达式对大写字母的拆分,无论如何我们的实现有点天真。此代码片段需要 System.ComponentModel.DescriptionAttribute (它也是枚举常量的装饰器),并且 enumType 是扩展方法的“this”参数,类型为 Enum:

var attr = enumType.GetType().GetField(enumType.ToString())
                 .GetCustomAttributes(typeof(DescriptionAttribute), false);
   if (attr.Length > 0)
      return ((DescriptionAttribute)attr[0]).Description;

My team had this issue in our recent project. We kept the enums, because they are the thing to use for a finite list of known constant values, but we did a few things to make them more developer-friendly:

  1. We decorated the enum values with [Description()] attributes containing the "friendly name" of each enum constant.
  2. We created a GetDescription() extension method that would reflectively examine the enum constant's Description attribute and return the friendly name. If it doesn't have one, the method attempts to insert spaces into the ToString() of the enum constant name. As CamelCased enum constants were a style requirement enforced by ReSharper, this worked for about 90% of our constants, and Description attributes handled the rest (mostly uppercase acronyms in the name).
  3. We created a ParseEnum() generic extension method for strings that basically wraps Enum.Parse() (which is oogly; requires specifying the Enum type in the method and then casting to it). I think it may be smart enough to try looking for description attributes first, or else we simply use the still-readable ToString() representation as the data value of the drop-down item.

So, given the following:

public enum MyItems
{
   [Description("Item One")]
   ItemOne,
   [Description("Item Two")]
   ItemTwo,
   [Description("Item Three")]
   ItemThree
}

we could populate a DropDownList with user-friendly choices in two lines:

foreach(MyValues value in Enum.GetValues<MyValues>())
   myDDL.Items.Add(New ListItem(value.GetDescription(), value.ToString())

... and then we could parse the selection back out with very readable code:

var theValue = myDDL.SelectedItem.Value.ParseEnum<MyValues>()

EDIT: I have been asked for the GetDescription() method. I'm a little iffy about sharing the whole thing, but here's the basic algorithm for getting a Description attribute that decorates an enum constant. Parsing a CamelCased name is pretty straightforward RegEx splits on capital letters, and our implementation's a little naive anyway. This snippet requires System.ComponentModel.DescriptionAttribute (which is also the decorator for the enum constants), and enumType is the "this" parameter of the extension method, of type Enum:

var attr = enumType.GetType().GetField(enumType.ToString())
                 .GetCustomAttributes(typeof(DescriptionAttribute), false);
   if (attr.Length > 0)
      return ((DescriptionAttribute)attr[0]).Description;
一个人的旅程 2024-09-23 23:11:12

如果您使用的变量具有有限且众所周知的可能状态数量,那么枚举确实是您可以使用的正确构造。有许多可能的方法可以使 UI 的使用更加方便,并且您引用了两个很好的方法,它们是类型转换器和扩展方法。

If you are working with a variable that has a finite and well-known number of possible states, then an enum is indeed the correct construct for you to use. There are many possible ways to make working with the UI more convenient, and you have cited two excellent ones, which are type converters and extension methods.

泅人 2024-09-23 23:11:12

类似枚举的构造绝对是正确的选择。如果由于某种原因您不想使用熟悉的内置方式,您可以使用自己的工具来实现更多功能。这是基本思想:

class Country {
    private static Country US;
    private static Country JP

    static Country() { //initialize static variables }
    private Country( string name ) { //initialize variables of Country instance }

    }

有一个设计模式,但我忘记了名字。

An Enum like construct is definitely the right choice. If for some reason you don't want to use the familiar built in way, you can make you're own with a bir more functionaliy. here's the basic idea:

class Country {
    private static Country US;
    private static Country JP

    static Country() { //initialize static variables }
    private Country( string name ) { //initialize variables of Country instance }

    }

There's a design pattern for this, but I forget the name.

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