Is it possible to iterate through properties of a custom enum?
My custom enum looks like this:
public class Sex
{
public static readonly Sex Female = new Sex("xx", "Female");
public static readonly Sex Male = new Sex("xy", "Male");
internal string Value { get; private set; }
internal string Description { get; private set; }
public override string ToString()
{
return Value.ToString();
}
public string GetDescription()
{
return this.Description;
}
protected Sex(string value, string description)
{
this.Value = value;
this.Description = description;
}
I use it to enable an enum to "enumerate" with strings i.e. "xx", "xy"...
My question is if it is possible to iterate though all sexes with the goal to fill a DropDownList ListItems with value and description.
var ddlSex = new DropDownList()
foreach(var sex in typeof(Sex).<some_magic>)
{
ddlSex.Items.Add(new ListItem(sex.ToString(), sex.GetDescription()));
}
My idea is to solve the problem with the System.Reflection library but I'm sure how.