c# - Count depth of a hierarchy of classes -


i've seen lot of different examples of how , aware write out loop iterates entire tree of classes find maximum depth, cannot think there has simpler way.

basically have 2 classes developed host applications settings, settinggroup sounds like, folder, , setting setting , configurations allow application know setting , how display it. reason dont use fixed class , write out field settings application plugin driven , wish settings remain centralized plugins added or removed , not have worry decentralized data plugins.

when dynamically creating settings page necessary know maximum depth of particular settinggroup know if root should first organized tabs, pages, sections etc...

long story short, there reasonably lightweight way determine groups maximum depth?

public enum descriptionvisibility { none, subtext, tooltip }; public enum settingtype { bool, integer, decimal, text }; public enum settingcontrol { checkbox, textbox, slider, radio, dropdown, textfield, color};  public class settinggroup {     public string name { get; set; }     public string description { get; set; }     public list<settinggroup> groups { get; set; }     public list<setting> settings { get; set; } } public class setting {     public string name { get; set; }     public string description { get; set; }     public descriptionvisibility descriptionvisibility { get; set; }     public dictionary<string, dynamic> configuration { get; set; }     public dynamic settingvalue { get; set; }     public settingtype settingtype { get; set; }     public settingcontrol settingcontrol { get; set; }  } 

edit: untested, considering using;

    private static int getdepth(this settinggroup group, int depth = 0)     {         if (group.groups == null)             return depth;         if (group.groups.count == 0)             return depth;          int returndepth = depth;          foreach (settinggroup subgroup in group.groups)         {             int subgroupdepth = subgroup.getdepth(depth + 1);             if (subgroupdepth > returndepth)                 returndepth = subgroupdepth;         }          return returndepth;     } 

its pretty basic couldn't slow, still seems bulky, there not linq way perhaps?


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -