I have an enum which has obsolete values. I would like to return a list of the enum values which are not obsolete. I can return a list of all enum values but I can't identify the obsolete values to exclude them from the list.
Here is some sample code to illustrate the issue. First, here is a sample enum with the second value marked obsolete:
public enum MyEnum
{
Item1 = 1,
[Obsolete]
Item2 = 2,
Item3 = 3
}
Here is a sample extension method that returns all values of the enum as a list:
public static class MyEnumExt
{
/// <summary>
/// I want to amend this method to return a list only including the
/// enum values that are not obsolete
/// </summary>
public static List<MyEnum> GetList(this MyEnum t)
{
return Enum.GetValues(t.GetType()).Cast<MyEnum>().ToList();
}
}
Does anyone have any suggestions for amending my extension method to return only values that are not marked obsolete?