背景:
项目中有大量的枚举型数值,已经运维了几年的时间,相当一部分是定义的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();
}
}
下次若需要了解新的枚举了,直接把新的枚举 贴到这里,可以直接运行,得到结果,非常方便。