title: C# - How to map object structure to a TreeView?
body: <p>i would like to map the structure of an object to a TreeView. The object itself consists of <code>Dictionary&lt;string,object&gt;</code>. The problem for me now is that there is no consistency in how deep the structure can be because every <code>object</code> can be another dictionary (or just a text). For example, this is how one object could look like:
  <a href="https://i.stack.imgur.com/ARvS9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ARvS9.png" alt="One Object"></a></p>
  
  <p>I know that i need some kind of recursion but i am not sure how i could map that to a tree view?
  This is my current code:</p>
  
  <pre><code>        Dictionary&lt;string, object&gt; dict = (Dictionary&lt;string, object&gt;)e.Node.Tag; //dict is the object
          foreach(KeyValuePair&lt;string,object&gt; kvp in dict)
          {
              if(kvp.Key != "icon" &amp;&amp; kvp.Key != "typeLine")
              {
  
              }
          }
  </code></pre>
  
  <p>Any help is very much appreciated!</p>
  
  <p>Edit: Sometimes the object of the KeyValuePair isn't another dictionary but just an array of objects (i dont't know why that is). It looks impossible, at least to me, to map that strange assembly of data to a treeview. Here is a picture about what i mean:</p>
  
  <p><a href="https://i.stack.imgur.com/m7x0q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m7x0q.png" alt="enter image description here"></a></p>
  
  <p>Edit(2): Okay, i think i finally got it working with this slightly modified function from Matthys:</p>
  
  <pre><code>        private TreeNode[] RecurseTree(Dictionary&lt;string, object&gt; dic)
      {
          List&lt;TreeNode&gt; branch = new List&lt;TreeNode&gt;();
  
          foreach (KeyValuePair&lt;string, object&gt; kvp in dic)
          {
              TreeNode twig = new TreeNode(kvp.Key);
              if (kvp.Value is string || kvp.Value is bool || kvp.Value is int)
              {
                  twig.Nodes.Add(new TreeNode(kvp.Value.ToString()));
              }
              else if (kvp.Value is Dictionary&lt;string, object&gt;)
              {
                  twig.Nodes.AddRange(RecurseTree((Dictionary&lt;string, object&gt;)kvp.Value));
              }
              else
              {
                  IEnumerable enumerable = (kvp.Value as IEnumerable);
                  if (enumerable != null)
                  {
                      foreach (var item in enumerable)
                      {
                          if (item is string || item is bool || item is int)
                          {
                              twig.Nodes.Add(new TreeNode(item.ToString()));
                          }
                          else if (item is Dictionary&lt;string, object&gt;)
                          {
                              twig.Nodes.AddRange(RecurseTree((Dictionary&lt;string, object&gt;)item));
                          }
                          else
                          {
                              IEnumerable en = (item as IEnumerable);
                              foreach(var i in en)
                                  twig.Nodes.Add(new TreeNode(i.ToString()));
                          }
                      }
                  }
              }
              branch.Add(twig);
          }
  
          return branch.ToArray();
      }
  </code></pre>