给定多个枚举,动态输出枚举值

背景:

   项目中有大量的枚举型数值,已经运维了几年的时间,相当一部分是定义的16进制数,但对应的数据库中存的是10进制的int型数值。


需要一个方法:

  可以动态输出对应的枚举值。


代码如下:

        private static T PrintEnumValue<T>()
        {
            #region ### test enum values

            Type item = typeof(T);
            Array Arrays_name = Enum.GetNames(item);
            Array Arrays = Enum.GetValues(item);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < Arrays.LongLength; i++)
            {
                sb.AppendLine();
                sb.Append(Arrays_name.GetValue(i) + ":");
                sb.Append((int)Arrays.GetValue(i));
            }

            Console.WriteLine(sb.ToString());

            #endregion

            return default(T);
        }

对应的枚举值:

   
        public enum ObjectState
        {
            /// <summary>
            /// The value indicating that object is normal.
            /// Value 0x0
            /// </summary>
         
            Normal = 0,
            /// <summary>
            /// The value indicating that object is deleted logically.
            /// Value 0x01
            /// </summary>
         
            Deleted = 0x01,
            /// <summary>
            /// The value indicating that object is invisible.
            /// Value 0x02
            /// </summary>
         
            Invisible = 0x02,
            /// <summary>
            /// The value indicating that object is readonly.
            /// Value 0x04
            /// </summary>
         
            ReadOnly = 0x04,
            /// <summary>
            /// The value indicating that object is Disabled.
            /// </summary>
         
            Disabled = 0x08,
            /// <summary>
            /// The value indicating that operation is succeed.
            /// Value 0x10
            /// </summary>   
         
            Succeed = 0x10,
            /// <summary>
            /// The value indicating that operation is failed.
            /// Value 0x11
            /// </summary>   
         
            Failed = 0x11,
            /// <summary>
            /// The value indicating that object or operation is pending.
            /// Value 0x100
            /// </summary>
         
            Pending = 0x100,
            /// <summary>
            /// The value indicating that object or operation is approved.
            /// Value 0x110
            /// </summary>
         
            Approved = 0x110,
            /// <summary>
            /// The value indicating that object or operation is rejected.
            /// Value 0x120
            /// </summary>
         
            Rejected = 0x120,
            /// <summary>
            /// The value indicating that operation is in process.
            /// Value 0x140
            /// </summary>
         
            InProcess = 0x140,
            /// <summary>
            /// The value indicating that object or operation is rejected.
            /// Value 0x180
            /// </summary>
         
            Completed = 0x180
        }

调用方法:

  class Program
    {
        static void Main(string[] args)
        {
            PrintEnumValue<ObjectState>();
            Console.Read();
        }
	}

下次若需要了解新的枚举了,直接把新的枚举 贴到这里,可以直接运行,得到结果,非常方便。




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值