【Enum】加载枚举值

/// <summary>
/// 获取枚举注释
/// </summary>
/// <typeparam name="T">枚举类型</typeparam>
/// <param name="e">枚举值</param>
/// <returns></returns>
public static string ToComment<T>(this T e) where T : struct, IConvertible
{
    var t = typeof(T);
    if (!t.IsEnum) return null;
    var name = t.GetEnumName(e);
    var fieldInfos = t.GetFields();
    foreach (var fieldInfo in fieldInfos)
    {
        if (fieldInfo.Name == name)
        {
            var attr = fieldInfo.GetCustomAttributes(typeof(EnumNameAttribute), false).FirstOrDefault();
            if (attr != null)
            {
                return ((EnumNameAttribute)attr).Comment;
            }
        }
    }
    return null;
}
/// <summary>
/// 订单状态
/// </summary>
public EOrderStatus Status { get; set; }
public string StatusName
{
    get
    {
        return ETL.Common.EnumHelper.ToComment(Status);
    }
}
/// <summary>
/// 订单状态
/// </summary>
public enum EOrderStatus : int
{
    /// <summary>
    /// 待支付
    /// </summary>
    [EnumName("待支付")]
    waitPay = 0,
    /// <summary>
    /// 支付中待确认(线下支付上传缴费凭证后,待财务审核)
    /// </summary>
    [EnumName("支付中待确认")]
    payingWaitVerify = 1,
    /// <summary>
    /// 退款中 (申请退款,待审核)当退款申请提交后,订单状态应由已支付变为退款中 大订单删除 明细不删
    /// </summary>
    [EnumName("退款中")]
    refunding = 2,

    /// <summary>
    /// 已退款  大订单删除 明细不删
    /// </summary>
    [EnumName("已退款")]
    refundComplete = 3,

    /// <summary>
    /// 已完成
    /// </summary>
    [EnumName("已完成")]
    complete = 4,

    /// <summary>
    /// 已取消
    /// </summary>
    [EnumName("已取消")]
    cancel = 5,
    /// <summary>
    /// 已驳回
    /// </summary>
    [EnumName("已驳回")]
    rejected = 6,

    /// <summary>
    /// 部分退款
    /// </summary>
    [EnumName("部分退款")]
    refundPart = 7,
}
View Code

 

posted @ 2022-07-12 10:57  智者见智  阅读(42)  评论(0编辑  收藏  举报