コード例 #1
0
        public Dictionary <string, string> Format(object item, int depth)
        {
            if (depth > MaxDepth || item == null)
            {
                return(new Dictionary <string, string>());
            }
            var result = new Dictionary <string, string>();
            var props  = ReflectionFactory.GetProperties(item);

            foreach (var prop in props)
            {
                // see if we have a parser for this property
                var parser = Formatters.GetFormatter(prop.PropertyType);
                if (parser == null)
                {
                    continue;
                }

                try
                {
                    var val = prop.GetValue(item);
                    if (val == null)
                    {
                        continue;
                    }
                    var propKeyValue = parser.Format(val, depth + 1);

                    foreach (var kv in propKeyValue)
                    {
                        var    suffix = string.IsNullOrWhiteSpace(kv.Key) ? string.Empty : "_" + kv.Key;
                        string key    = prop.Name + suffix;
                        string value;
                        if (key.Length > 4000)
                        {
                            key = kv.Key.Substring(0, 4000); //short circuit strings larger than 8kb
                        }
                        if (!string.IsNullOrEmpty(kv.Value) && kv.Value.Length > 4000)
                        {
                            value = kv.Value.Substring(0, 4000);
                        }
                        else
                        {
                            value = kv.Value;
                        }
                        result[key] = value;
                    }
                }
                catch (Exception e) { } // swallow exception
            }

            return(result);
        }