使用自定义属性对枚举进行foreach
这个问题在这里已经有了答案:
你的意思是这样吗?
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