java 枚举值属性_获取枚举值的属性

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

using System.ComponentModel; // for DescriptionAttribute

enum FunkyAttributesEnum

{

[Description("Name With Spaces1")]

NameWithoutSpaces1,

[Description("Name With Spaces2")]

NameWithoutSpaces2

}

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

价值很容易:

Array values = System.Enum.GetValues(typeof(FunkyAttributesEnum));

foreach (int value in values)

Tuple.Value = Enum.GetName(typeof(FunkyAttributesEnum), value);

但是,如何获取描述属性的值以填充Tuple.Desc? 我可以考虑如果Attribute属于枚举本身,那么该怎么做,但是我对如何从枚举的值中获取它感到困惑。

#1楼

您还可以定义一个枚举值,例如Name_Without_Spaces ,当需要描述时,请使用Name_Without_Spaces.ToString().Replace('_', ' ')用空格替换下划线。

#2楼

我实现了此扩展方法,以从枚举值获取描述。 它适用于所有枚举。

public static class EnumExtension

{

public static string ToDescription(this System.Enum value)

{

FieldInfo fi = value.GetType().GetField(value.ToString());

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

return attributes.Length > 0 ? attributes[0].Description : value.ToString();

}

}

#3楼

除了AdamCrawford响应之外 ,我还创建了一种更专业的扩展方法,将其提要以获取描述。

public static string GetAttributeDescription(this Enum enumValue)

{

var attribute = enumValue.GetAttributeOfType();

return attribute == null ? String.Empty : attribute.Description;

}

因此,为了获得描述,您可以使用原始扩展方法作为

string desc = myEnumVariable.GetAttributeOfType().Description

或者您可以简单地将扩展方法调用为:

string desc = myEnumVariable.GetAttributeDescription();

希望可以使您的代码更具可读性。

#4楼

这是从Display属性获取信息的代码。 它使用通用方法检索属性。 如果找不到该属性,它将枚举值转换为字符串,而pascal / camel大小写转换为标题大小写( 在此处获得代码)

public static class EnumHelper

{

// Get the Name value of the Display attribute if the

// enum has one, otherwise use the value converted to title case.

public static string GetDisplayName(this TEnum value)

where TEnum : struct, IConvertible

{

var attr = value.GetAttributeOfType();

return attr == null ? value.ToString().ToSpacedTitleCase() : attr.Name;

}

// Get the ShortName value of the Display attribute if the

// enum has one, otherwise use the value converted to title case.

public static string GetDisplayShortName(this TEnum value)

where TEnum : struct, IConvertible

{

var attr = value.GetAttributeOfType();

return attr == null ? value.ToString().ToSpacedTitleCase() : attr.ShortName;

}

///

/// Gets an attribute on an enum field value

///

/// The enum type

/// The type of the attribute you want to retrieve

/// The enum value

/// The attribute of type T that exists on the enum value

private static T GetAttributeOfType(this TEnum value)

where TEnum : struct, IConvertible

where T : Attribute

{

return value.GetType()

.GetMember(value.ToString())

.First()

.GetCustomAttributes(false)

.OfType()

.LastOrDefault();

}

}

这是用于转换为标题大小写的字符串的扩展方法:

///

/// Converts camel case or pascal case to separate words with title case

///

///

///

public static string ToSpacedTitleCase(this string s)

{

//https://stackoverflow.com/a/155486/150342

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;

TextInfo textInfo = cultureInfo.TextInfo;

return textInfo

.ToTitleCase(Regex.Replace(s,

"([a-z](?=[A-Z0-9])|[A-Z](?=[A-Z][a-z]))", "$1 "));

}

#5楼

这应该做您需要的。

var enumType = typeof(FunkyAttributesEnum);

var memberInfos = enumType.GetMember(FunkyAttributesEnum.NameWithoutSpaces1.ToString());

var enumValueMemberInfo = memberInfos.FirstOrDefault(m => m.DeclaringType == enumType);

var valueAttributes =

enumValueMemberInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

var description = ((DescriptionAttribute)valueAttributes[0]).Description;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的两个引用,可以看出这是在Java中使用枚举类型的两种不同方法来获取枚举内容。下面是两种方法的详细解释和示例代码: 1.使用EnumUtil.getEnumObject()方法获取枚举内容 这种方法需要使用一个名为EnumUtil的工具类,该类提供了一个getEnumObject()方法,该方法接受两个参数:枚举类型和一个Lambda表达式,该表达式用于比较枚举类型中的某个属性是否与提供的相等。如果找到匹配的枚举,则返回该枚举对象,否则返回null。 ```java Optional m1 = EnumUtil.getEnumObject(PurchaseDemandEnum.class, e -> e.getCode().equals(code)); ``` 其中,PurchaseDemandEnum是枚举类型的名称,getCode()是枚举类型中的一个方法,用于获取枚举类型中的code属性,code是枚举类型中的一个属性,表示枚举类型的。Lambda表达式e -> e.getCode().equals(code)用于比较枚举类型中的code属性是否与提供的code相等。 2.使用枚举类型中的方法获取枚举内容 这种方法需要在枚举类型中定义一个方法,该方法接受一个参数,用于比较枚举类型中的某个属性是否与提供的相等。如果找到匹配的枚举,则返回该枚举对象,否则返回null。 ```java Integer codeByName = PurchaseDemandEnum.getCodeByName(name); ``` 其中,PurchaseDemandEnum是枚举类型的名称,getCodeByName()是枚举类型中的一个方法,用于获取枚举类型中的name属性,name是枚举类型中的一个属性,表示枚举类型的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值