使用自定义属性对枚举进行foreach

这个问题在这里已经有了答案:

  • 你可以遍历所有的枚举值? [复制] 8个答案

  • 你的意思是这样吗?

        class Program
        {
            static void Main()
            {
                foreach (var field in typeof(TableName).GetFields(BindingFlags.Static | BindingFlags.Public))
                {
                    ProcessField(field);
                }
            }
    
            static void ProcessField(FieldInfo field)
            {
                ProcessD(field.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute);
                ProcessDWV(field.GetCustomAttribute(typeof(DescriptionWithValueAttribute)) as DescriptionWithValueAttribute);            
            }
    
            static void ProcessD(DescriptionAttribute attribute)
            {
                if(attribute != null)
                {
                    //...
                }
            }
    
            static void ProcessDWV(DescriptionWithValueAttribute attribute)
            {
                if (attribute != null)
                {
                    //...
                }
            }
    
    链接地址: http://www.djcxy.com/p/91497.html

    上一篇: foreach on enum with Custom Attributes

    下一篇: How do I iterate through an enum?