NETCORE - enum 枚举
NETCORE - enum 枚举
环境 .net6 webapi
1. 创建枚举
internal enum FieldContrastPropertytypes { Id = 1, Type = 2, UnitTypeId = 3, Name = 4, ClassName = 5, UnitType = 6 }
2. 创建帮助类
internal class EnumAssist { public static List<TEnum> EnumValues<TEnum>() where TEnum : struct, IComparable, IFormattable, IConvertible { var enumType = typeof(TEnum); if (!enumType.IsEnum) { throw new ArgumentException(); } return Enum.GetValues(enumType).Cast<TEnum>().ToList(); } }
3. 使用
var enumArr = EnumAssist.EnumValues<FieldContrastPropertytypes>();
跟据编码获取名称
例:
public enum EnumProxyType { None = 0, Layer = 1, Asset = 2, Founctional = 4, Entity = 8, Shape = 16, LibStander = 32, RootNode = 64, Group = 128, TileContent = 256, TilingGroup = 512, TilingGroupHandler = 1024, TilingContentHandler = 2048, OpenDesign = 4096, NodeProxy = 8192, } var name=Enum.GetName(typeof(EnumProxyType), 4)
引用:https://www.coder.work/article/969924
引用:https://stackoverflow.com/questions/23794691/extension-method-to-get-the-values-of-any-enum