interlace /source/library/Interlace.Pinch/Interlace.Pinch.Dynamic/DynamicStructure.cs

Language C# Lines 112
MD5 Hash eff8e3231a0aae5d872eb9ea470e549b Estimated Cost $2,206 (why?)
Repository https://bitbucket.org/VahidN/interlace.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Interlace.Pinch.Dynamic
{
    public class DynamicStructure
    {
        string _name;
        string _identifier;
        Dictionary<string, DynamicMember> _members;
        List<string> _orderedMemberNames;

        public DynamicStructure(string name, string identifier)
        {
            _name = name;
            _identifier = identifier;
            _members = new Dictionary<string, DynamicMember>();
            _orderedMemberNames = new List<string>();
        }

        public object this[string name]
        {
            get { return _members[name].Value; }
        }

        public string Name
        {
            get { return _name; }
        }

        public Dictionary<string, DynamicMember> Members
        {
            get { return _members; }
        }

        public List<string> OrderedMemberNames
        {
            get { return _orderedMemberNames; }
        }

        public string Dumped
        {
            get
            {
                StringBuilder builder = new StringBuilder();

                Dump(builder);

                return builder.ToString();
            }
        }

        public void Dump(StringBuilder builder)
        {
            Dump(builder, 0);
        }

        internal void Dump(StringBuilder builder, int indent)
        {
            string indentString = "".PadLeft(indent * 2);

            builder.AppendFormat("{0}({1})", indentString, _identifier);
            builder.AppendLine();

            foreach (string name in _orderedMemberNames)
            {
                if (!_members.ContainsKey(name)) continue;

                DynamicMember member = _members[name];

                builder.AppendFormat("{0}{1}: ", indentString, name);

                if (member.Value is List<object>)
                {
                    foreach (object value in (List<object>)member.Value)
                    {
                        builder.AppendFormat("{0}  ", indentString);

                        DumpValue(builder, indent, value);
                    }
                }
                else
                {
                    DumpValue(builder, indent, member.Value);
                }
            }
        }

        void DumpValue(StringBuilder builder, int indent, object value)
        {
            if (value is DynamicStructure)
            {
                builder.AppendLine();

                ((DynamicStructure)value).Dump(builder, indent + 1);
            }
            else if (value is byte[])
            {
                builder.AppendFormat("({0} bytes)", ((byte[])value).Length);
                builder.AppendLine();
            }
            else
            {
                builder.AppendFormat("{0}", value);
                builder.AppendLine();
            }
        }
    }
}
Back to Top