C#枚举比较死板,没有Java里自由,有些需求要特别对待。
public struct EnumItem { public int Value; public string Desc; public EnumItem(int value, string desc) { Value = value; Desc = desc; } public static EnumItem FromInt(int val, List<EnumItem> enums) { foreach (var item in enums) { if (item.Value == val) { return item; } } throw new NotSupportedException("枚举值不在范围内"); } } public struct OrderStatus { public readonly static EnumItem WaitPay = new EnumItem(1, "待付款"); public readonly static EnumItem WaitShip = new EnumItem(2, "待发货"); public readonly static EnumItem WaitReceive = new EnumItem(3, "待收货"); public readonly static EnumItem Received = new EnumItem(4, "已收货"); public static List<EnumItem> GetItems() { return new List<EnumItem>() { WaitPay, WaitShip, WaitReceive, Received }; } }
标签:EnumItem,C#,readonly,枚举,static,new,public From: https://www.cnblogs.com/jonney-wang/p/17105488.html