Пример #1
0
        private TreeNode AddNode(object data, TreeNode rootNode)
        {
            TreeNode node = null;

            if (rootNode == null)
            {
                rootNode = new TypedTreeNode("root", null);
                node     = rootNode;
            }
            if (IsGenericType(typeof(Dictionary <,>)).Invoke(data) &&
                HasGenericArgTypes(typeof(string), typeof(object)).Invoke(data))
            {
                node = new TypedTreeNode("Dictionary", typeof(Dictionary <,>));
                rootNode.Nodes.Add(node);
                foreach (KeyValuePair <string, object> pair in (Dictionary <string, object>)data)
                {
                    AddNode(pair, node);
                }
            }
            else if (IsGenericType(typeof(List <>)).Invoke(data) &&
                     HasGenericArgTypes(typeof(object)).Invoke(data))
            {
                node = new TypedTreeNode("List", typeof(List <>));
                rootNode.Nodes.Add(node);
                foreach (object o in (List <object>)data)
                {
                    AddNode(o, node);
                }
            }
            else if (IsGenericType(typeof(KeyValuePair <,>)).Invoke(data) &&
                     HasGenericArgTypes(typeof(string), typeof(object)).Invoke(data))
            {
                KeyValuePair <string, object> pair = (KeyValuePair <string, object>)data;
                node = new TypedTreeNode(pair.Key, typeof(KeyValuePair <,>));
                rootNode.Nodes.Add(node);
                AddNode(pair.Value, node);
            }
            else if (!data.GetType().IsGenericType)
            {
                node = new TypedTreeNode(data.ToString(), data.GetType());
                rootNode.Nodes.Add(node);
            }
            else
            {
                throw new Exception("Whoops");
            }

            return(node);
        }
Пример #2
0
 private dynamic TreeToByml(TypedTreeNode rootNode)
 {
     if (rootNode == null)
     {
         return(null);
     }
     if (rootNode.Type == typeof(List <>))
     {
         List <TypedTreeNode> nodes = new List <TypedTreeNode>();
         foreach (TypedTreeNode node in rootNode.Nodes)
         {
             nodes.Add(node);
         }
         return(nodes.ConvertAll <dynamic>(node => TreeToByml(node)));
     }
     else if (rootNode.Type == typeof(Dictionary <,>))
     {
         List <TypedTreeNode> nodes = new List <TypedTreeNode>();
         foreach (TypedTreeNode node in rootNode.Nodes)
         {
             nodes.Add(node);
         }
         return(nodes.ConvertAll <KeyValuePair <string, dynamic> >(node => TreeToByml(node))
                .ToDictionary(pair => pair.Key, pair => pair.Value));
     }
     else if (rootNode.Type == typeof(KeyValuePair <,>))
     {
         return(new KeyValuePair <string, dynamic>(rootNode.Text, TreeToByml((TypedTreeNode)rootNode.Nodes[0])));
     }
     else if (!rootNode.Type.IsGenericType)
     {
         return(Convert.ChangeType(rootNode.Text, rootNode.Type));
     }
     else
     {
         throw new Exception("Double Whoops!");
     }
 }