c# - Display different values of one enumerator list into different sub menu of menu list -
i have enum follows [it prototype]
public enum class alphabets { none=0, a, e, i, o, u, b, c, d, f, g, noun, adj, verb, nosign };
i want display enum contents menu item such that,
main menu "english" contains sublist vowels, letters, grammer
, these sublist contains element follows,
english
- vowels - a, e, i, o, u
- letters - b, c, d, f, g,
- grammer - noun, adj, verb
possible have 1 enum , sort across different sub menu of menu list or different menu list?
you can notate each enum display groupname when enumerate through them, groupname , sort accordingly
i use these extension methods values
//gets attributes enum public static t getattribute<t>(this enum value) t : attribute { if (value == null) { throw new argumentnullexception(); } var type = value.gettype(); var memberinfo = type.getmember(value.tostring()); var attributes = memberinfo[0].getcustomattributes(typeof(t), false); return (t)attributes[0]; } //this pull displaygroup display attribute public static string displaygroup(this enum value) { var attribute = value.getattribute<displayattribute>(); return attribute == null ? value.tostring() : attribute.groupname; } //enum decoration looks public enum myenum { [display(groupname = "mygroupname")] anenum } //pull group name var groupname = myenum.anenum.displaygroup(); //assert.istrue(groupname.equals("mygroupname");
Comments
Post a Comment