Created
August 22, 2013 14:49
-
-
Save rstackhouse/6308220 to your computer and use it in GitHub Desktop.
Getting enum values with specific attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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