标签: enums

获得Enum价值的属性

我想知道是否可以获取枚举值的属性而不是枚举本身的属性?例如,假设我有以下枚举:

using System.ComponentModel; // for DescriptionAttribute

enum FunkyAttributesEnum
{
    [Description("Name With Spaces1")]
    NameWithoutSpaces1,    
    [Description("Name With Spaces2")]
    NameWithoutSpaces2
}
Run Code Online (Sandbox Code Playgroud)

我想要的是枚举类型,产生2元组的枚举字符串值及其描述.

价值很容易:

Array values = System.Enum.GetValues(typeof(FunkyAttributesEnum));
foreach (int value in values)
    Tuple.Value = Enum.GetName(typeof(FunkyAttributesEnum), value);
Run Code Online (Sandbox Code Playgroud)

但是如何获取描述属性的值,以填充Tuple.Desc?如果属性属于枚举本身,我可以想到如何做到这一点,但我不知道如何从枚举的值中获取它.

c# reflection enums .net-attributes

453
推荐指数
9
解决办法
27万
查看次数

如何将RadioButtons绑定到枚举?

我有这样的枚举:

public enum MyLovelyEnum
{
    FirstSelection,
    TheOtherSelection,
    YetAnotherOne
};
Run Code Online (Sandbox Code Playgroud)

我的DataContext中有一个属性:

public MyLovelyEnum VeryLovelyEnum { get; set; }
Run Code Online (Sandbox Code Playgroud)

我的WPF客户端中有三个RadioButton.

<RadioButton Margin="3">First Selection</RadioButton>
<RadioButton Margin="3">The Other Selection</RadioButton>
<RadioButton Margin="3">Yet Another one</RadioButton>
Run Code Online (Sandbox Code Playgroud)

现在如何将RadioButtons绑定到属性以进行正确的双向绑定?

data-binding wpf enums radio-button

400
推荐指数
4
解决办法
13万
查看次数

为什么枚举类优于普通枚举?

我听说有些人因为类型安全而建议在C++中使用枚举.

但这究竟意味着什么?

c++ enums class c++-faq

376
推荐指数
8
解决办法
22万
查看次数

值的枚举字符串名称

我有一个像这样的枚举构造:

public enum EnumDisplayStatus
{
    None    = 1,
    Visible = 2,
    Hidden  = 3,
    MarkedForDeletion = 4
}
Run Code Online (Sandbox Code Playgroud)

在我的数据库中,枚举由值引用.我的问题是,如何将枚举的数字表示形式转回字符串名称.

例如,给定2结果应该是Visible.

c# enums

370
推荐指数
12
解决办法
45万
查看次数

枚举"继承"

我在低级命名空间中有一个枚举.我想在一个"继承"低级枚举的中级命名空间中提供一个类或枚举.

namespace low
{
   public enum base
   {
      x, y, z
   }
}

namespace mid
{
   public enum consume : low.base
   {
   }
}
Run Code Online (Sandbox Code Playgroud)

我希望这是可能的,或者某种类可以代替枚举消耗,它将为枚举提供一层抽象,但仍然让该类的实例访问枚举.

思考?

编辑:我之前没有将其转换为类中的consts的原因之一是我必须使用的服务需要低级枚举.我得到了WSDL和XSD,它们将结构定义为枚举.该服务无法更改.

.net c# enums

370
推荐指数
8
解决办法
21万
查看次数

使用枚举值作为字符串文字

使用存储在Enum中的值作为字符串文字的最佳方法是什么?例如:

public enum Modes {
    some-really-long-string,
    mode1,
    mode2,
    mode3
}
Run Code Online (Sandbox Code Playgroud)

然后我可以用它Mode.mode1来返回它的字符串表示mode1.无需继续通话Mode.mode1.toString().

java string enums

365
推荐指数
12
解决办法
62万
查看次数

如何从价值中获取C#Enum描述?

可能重复:
获取Enum值的属性

我有一个带有Description属性的枚举,如下所示:

public enum MyEnum
{
    Name1 = 1,
    [Description("Here is another")]
    HereIsAnother = 2,
    [Description("Last one")]
    LastOne = 3
}
Run Code Online (Sandbox Code Playgroud)

我找到了一些用于根据Enum检索描述的代码

public static string GetEnumDescription(Enum value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    DescriptionAttribute[] attributes = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];

    if (attributes != null && attributes.Any())
    {
        return attributes.First().Description;
    }

    return value.ToString();
}
Run Code Online (Sandbox Code Playgroud)

这允许我编写如下代码:

var myEnumDescriptions = from MyEnum n in Enum.GetValues(typeof(MyEnum))
                         select new { ID = (int)n, Name = Enumerations.GetEnumDescription(n) };
Run Code Online (Sandbox Code Playgroud)

我想要做的是,如果我知道枚举值(例如1) - 我该如何检索描述?换句话说,如何将整数转换为"枚举值"以传递给我的GetDescription方法?

c# enums

364
推荐指数
5
解决办法
41万
查看次数

创建字符串枚举的最佳方法是什么?

使enum类型表示一组字符串的最佳方法是什么?

我试过这个:

enum Strings{
   STRING_ONE("ONE"), STRING_TWO("TWO")
}
Run Code Online (Sandbox Code Playgroud)

我怎么能用它们Strings呢?

java enums

350
推荐指数
7
解决办法
45万
查看次数

在Java中将Int转换为枚举

在给定以下枚举的情况下,将Int转换为枚举的正确方法是什么?

public enum MyEnum
{
    EnumValue1,
    EnumValue2
}


MyEnum enumValue = (MyEnum) x; //Doesn't work???
Run Code Online (Sandbox Code Playgroud)

java enums casting ordinal

317
推荐指数
11
解决办法
25万
查看次数

Ruby中的枚举

在Ruby中实现枚举习惯的最佳方法是什么?我正在寻找一些我可以使用(几乎)像Java/C#枚举的东西.

ruby enums

312
推荐指数
11
解决办法
17万
查看次数