gpt4 book ai didi

c# - 从枚举中获取 DescriptionAttribute

转载 作者:行者123 更新时间:2023-11-30 16:19:32 24 4
gpt4 key购买 nike

<分区>

我有一个枚举,用于查找协调字符串值。其中一个枚举中有一个空格,因此我试图使用 description 属性来查找该值。找到 DescriptionAttribute 后,我无法转换回公共(public)类。

public class Address
{
...blah...more class datatypes here...

public AddressType Type { get; set; }

...blah....

public enum AddressType
{
FRA = 0,
JAP = 1,
MEX = 2,
CAN = 3,
[Description("United States")]
UnitedStates = 4,

}


if (Address.Type.ToString() == "UnitedStates")
{
Adddress.Type = GetDescription(Address.Type);
}

private static AddressType GetDescription(AddressType addrType)
{
FieldInfo fi = addrType.GetType().GetField(addrType.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute), false);
return (attributes.Length > 0) ? attributes[0].Description : addrType.ToString();
}

在 GetDescription 方法中,如何将其转换回其公共(public)类数据类型“AddressType”,但失败了,因为这里是一个字符串?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com