c# - How should I represent hierarchical flag enums? -


i have following set of enums:

[flags] public enum categories : uint {     = (1 << 0),     b = (1 << 1),     b1 = b | (1 << 16),     b2 = b | (1 << 17),     b3 = b | (1 << 18),     b4 = b | (1 << 19),     b5 = b | (1 << 20),     c = (1 << 2),     c1 = c | (1 << 21),     d = (1 << 3),     d1 = d | (1 << 22),     d2 = d | (1 << 23),     e = (1 << 4),     f = (1 << 5),     f1 = f | (1 << 23),     f2 = f | (1 << 24),     f3 = f | (1 << 25),     f4 = f | (1 << 26),     f5 = f | (1 << 27),     g = (1 << 6),     h = (1 << 7),     h1 = h | (1 << 28), } 

the idea enums represent hierarchical structure child enum implies parent , number of flags can applied.

the problem seeing child enums not being represented during debugging names or sets of names. i.e., categories.f = "f" categories.f2 = 16777248. have hoped categories.f2 = "f, f2" or @ least "f2"

how can make enums remain recognized flags? there better way accomplish i'm trying do?

it's strange value in debugger different tostring value. according documentation, 2 should match (because enum type indeed override tostring).

if c# object has overridden tostring(), debugger call override , show result instead of standard {<typename>}.

obviously not working enums. best guess debugger trying special, undocumented handling of enum types. adding debuggerdisplayattribute apparently resolves issue overriding behavior.

[debuggerdisplay("{tostring()}")] [flags] public enum categories : uint {     ... } 

categories.f2.tostring() = "f, f2"

c# won't magic you, because f2 has it's own name in enum. can manually mark the individual members this:

public enum categories {     [description("f, f2")]     f2 = f | (1 << 24), } 

and write code convert description.

public static string todescription(this categories c) {     var field = typeof(categories).getfield(c.tostring());     if (field != null)     {         return field.getcustomattributes().cast<descriptionattribute>().first().description;     } } ... categories.f2.todescription() == "f, f2"; 

or bit of magic generate yourself:

public static string todescription(this categories c) {     var categorynames =         v in enum.getvalues(typeof(categories)).cast<category>()         v & c == c         orderby v         select v.tostring();     return string.join(", ", categorynames); } 

unfortunately, extension method cannot used debuggerdisplayattribute, can use debuggertypeattribute, ymmv try this:

[debuggertype("categorydebugview")] [flags] public enum categories : uint {     ... }  internal class categorydebugview {     private category value;      public categorydebugview(category value)     {         this.value = value;     }      public override string tostring()     {         var categorynames =             v in enum.getvalues(typeof(categories)).cast<category>()             v & c == c             orderby v             select v.tostring();         return string.join(", ", categorynames);     } } 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -