Skip to content

Instantly share code, notes, and snippets.

@rstackhouse
Created August 22, 2013 14:49
Show Gist options
  • Save rstackhouse/6308220 to your computer and use it in GitHub Desktop.
Save rstackhouse/6308220 to your computer and use it in GitHub Desktop.
Getting enum values with specific attributes
void Main()
{
List<Days> weekend = new List<Days>();
Enum.GetValues(typeof(Days)).Cast<Days>().ToList()
.ForEach(d => {
var fi = d.GetType().GetField(d.ToString());
if (fi.GetCustomAttributes(typeof(WeekendAttribute), false).Length > 0)
{
weekend.Add(d);
}
});
weekend.Dump();
}
public class WeekendAttribute : Attribute
{
}
enum Days
{
[Weekend]
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
[Weekend]
Saturday
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment