views:

116

answers:

3

I have an enumeration for Status for a Task. Some of the statuses are considered obsolete, and I have marked them as obsolete, as seen below:

public enum TaskStatus
{
    [Description("")]
    NotSet = 0,
    Pending = 1,
    Ready = 2,
    Open = 3,
    Completed = 4,
    Closed = 5,
    [Description("On Hold")][Obsolete]
    OnHold = 6,
    [Obsolete]
    Canceled = 7
}

In my user interface I populate a drop down with values on the enumerations, but I want to ignore ones that are marked as obsolete. How would I got about doing this?

A: 

You can use the DebuggerHiddenAttribute and I know there is one that makes it hide from the properties explorer, but can't seem to remember the name.

masfenix
+3  A: 

You could write a LINQ-query:

var availableTaks = typeof (TaskStatus).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public)
    .Where(f => f.GetCustomAttributes(typeof (ObsoleteAttribute), false).Length == 0);
foreach(var task in availableTaks)
    Console.WriteLine(task);
Cristian Libardo
Thank you! Worked perfectly!
mattruma
+1  A: 
Type enumType = typeof(testEnum);
enumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public)[i].GetCustomAttributes(true);

Then you can use your choice of method to loop through the array and checking if there are any custom attributes.

JoshBerke
heh looks like I was real slow this time...the linq method is nice;-)
JoshBerke
welcome and thank you...another neat usage of this I found was to store a display name...So you could display On Hold instead of OnHold. Also if you want to localize it then you just store the key for the resource file...
JoshBerke