【问题标题】:EnumHelper returns description instead Enum valueEnumHelper 返回描述而不是枚举值
【发布时间】:2019-11-08 17:28:56
【问题描述】:

我使用EnumHelper 方法并尝试获取描述和enum 值(Id),如下所示:

枚举助手:

public static class MyEnumHelper
{
    public static string GetDescription<T>(this T enumerationValue)
        where T : struct
    {
        System.Type type = enumerationValue.GetType();
        if (!type.IsEnum)
        {
            throw new ArgumentException("Must be Enum type", "enumerationValue");
        }
        //for the enum
        MemberInfo[] memberInfo = type.GetMember(enumerationValue.ToString());
        if (memberInfo != null && memberInfo.Length > 0)
        {
            object[] attrs = memberInfo[0]
                .GetCustomAttributes(typeof(DescriptionAttribute), false);

            if (attrs != null && attrs.Length > 0)
            {
                return ((DescriptionAttribute)attrs[0]).Description;
            }
        }
        return enumerationValue.ToString();
    }
}

枚举:

public enum StatusEnum
{
    [Description("Deleted")]
    Deleted= 0,

    [Description("Active")]
    Active= 1,

    [Description("Passive")]
    Passive= 2
}


实体:

public class DemoEntity
{
    public int Id { get; set; }

    public StatusEnum StatusId { get; set; }

    [NotMapped]
    public string StatusName
    {
        get { return MyEnumHelper.GetDescription(StatusId); }
    }
}

控制器:

DemoEntity entity = DemoEntity();
entity.StatusId = StatusEnum.Passive; 
// !!! This returns "Passive" instead of its value 2. How can I get its value?

但是,当尝试使用如上所示的enum 的强类型功能为enum 分配Id 值时,我仍然得到它的描述而不是Id。知道问题出在哪里吗?

【问题讨论】:

  • 我认为这是描述的重点,以字符串格式显示值。号码还在后面。如果你真的想看这个数字,你可以把它转换成一个整数。
  • (int)StatusEnum.Passive 应该会为您提供整数值。
  • @JohnPete。投票赞成。在这种情况下,Entity 和 Enum 定义中的这种方法没有问题,我只需在需要枚举值时将其转换为 int 值。真的吗?或者您有没有比这更好的建议?
  • @the_lotus。在这种情况下,Entity 和 Enum 定义中的这种方法没有问题,我只需在需要枚举值时将其转换为 int 值。真的吗?或者您有没有比这更好的建议?
  • 我个人认为没有必要;提供东西意味着人们倾向于使用它们,并且当使用 int 而不是 enum 时会导致质量较差的代码。例如,这行代码是什么意思:if(activity.Phase == 2 || (activity.Phase == 3 &amp;&amp; (activity.ProtectionModes &amp; 1 == 1)))?是的..除非你知道这些数字是什么,否则它没有任何意义。比较if(activity.Phase == Phase.Starting || (activity.Phase == Phase.Running &amp;&amp; (activity.ProtectionModes &amp; Modes.CanAutoShutdown == Modes.CanAutoShutDown))) - 您可以立即知道此if 运行的条件

标签: c# asp.net asp.net-mvc asp.net-core enums


【解决方案1】:

如果您需要该值,只需将您的枚举转换为 int

entity.Id = (int)StatusEnum.Passive; 

【讨论】:

  • 正如 JohnPete22 在您之前回答的那样,我选择他的回答作为第一个回答。但我也投票赞成你的答案;)谢谢......
  • @hexadecimal JohnPete 发表了评论;他没有回答这个问题,并且支持评论并没有多大用处。 CharlesP 提供了答案; JohnPete 不应该在评论中提供答案,因为那不是 SO cmets 的用途(评论框中的占位符文本甚至说“避免在 cmets 中回答问题”)
【解决方案2】:

我可能会这样做:

public enum Status
{
    Deleted= 0,
    Active= 1,
    Passive= 2
}

public class DemoEntity
{
    public int Id { get; set; }

    public Status Status { get; set; }

    [NotMapped]
    public string StatusName
    {
        get { return this.Status.ToString("g"); }
    }

    [NotMapped]
    public int StatusId
    {
        get { return (int)this.Status; }
    }
}
  • 不要在您的枚举上添加“Enum”后缀 - 您不会为类(DemoEntityClass?)这样做,并且 Intellisense 会在类和枚举(例如)之间做出明确区分。微软不这样做(StringSplitOptions,不是 StringSplitOptionsEnum)
  • 如果您的描述属性与您的枚举名称值(与您的一样)相同,则 ToString("g") 是一种更简单的方法,可以将枚举名称转换为字符串
  • 枚举被命名为整数;整数可以随意转换为枚举。如果您尝试将 int 强制转换为 enum 并且它不是 enum 的成员,它只会继续表现为相同的 int 值(((Status)3).ToString() 返回“3”);

如果您对枚举工作感到厌烦,请考虑使用库 Enums.Net

【讨论】:

  • 谢谢,但抛出 'Status' is a type, which is not valid in the given context 错误。
  • 您确定没有错过this 吗?我的代码工作正常,看:dotnetfiddle.net/p6gOA0
  • 抱歉,打错了。非常感谢您的帮助,投了赞成票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
相关资源
最近更新 更多