Skip to content

Instantly share code, notes, and snippets.

@peteraritchie
Created October 11, 2012 14:09
Show Gist options
  • Save peteraritchie/3872561 to your computer and use it in GitHub Desktop.
Save peteraritchie/3872561 to your computer and use it in GitHub Desktop.
convert enum value to SelectList of all enum's values
public static SelectList ToSelectList<T>(this T value) where T : struct
{
if(!value.GetType().IsEnum) throw new ArgumentException("bad arg", "value");
var c = Enum.GetValues(value.GetType());
return new SelectList(c);
}
@nportelli
Copy link

That is similar to what I was playing around with but you still need to call it like
var x = MyEnum.SomeValue.ToSelectList();

I was hoping to just say
var x = MyEnum.ToSelectList(); or var x = EnumEx.ToSelectList(MyEnum);

But it doesn't let you. I think I understand why...but I still think it's dumb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment