C#遍历获取枚举的DescriptionAttribute内容

   var typeNames = new List<string>();
            var enumType = typeof(MouduleType);
            foreach (var value in Enum.GetValues(enumType))
            {
    
    
                var fieldInfo = enumType.GetField(Enum.GetName(enumType, value));

                var descriptionAttribute =
                    fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute), false) as DescriptionAttribute;
                if (descriptionAttribute != null) typeNames.Add(descriptionAttribute.Description);
            }

猜你喜欢

转载自blog.csdn.net/q913777031/article/details/121415422
今日推荐