Ejemplo n.º 1
0
        public static bool DumpedCollection(
            this TextWriter writer,
            IEnumerable sequence,
            DumpAttribute dumpAttribute,
            Action <object> dumpObject,
            Action indent,
            Action unindent)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (sequence == null)
            {
                throw new ArgumentNullException(nameof(sequence));
            }
            if (dumpAttribute == null)
            {
                throw new ArgumentNullException(nameof(dumpAttribute));
            }
            if (dumpObject == null)
            {
                throw new ArgumentNullException(nameof(dumpObject));
            }
            if (indent == null)
            {
                throw new ArgumentNullException(nameof(indent));
            }
            if (unindent == null)
            {
                throw new ArgumentNullException(nameof(unindent));
            }

            var sequenceType = sequence.GetType();
            var elementsType = sequenceType.IsArray
                                    ? new Type[] { sequenceType.GetElementType() }
                                    : sequenceType.IsGenericType
                                        ? sequenceType.GetGenericArguments()
                                        : new Type[] { typeof(object) };
            int count = 0;

            if (sequenceType.IsArray)
            {
                var piLength = sequenceType.GetProperty(nameof(Array.Length), BindingFlags.Instance | BindingFlags.Public);

                count = (int)piLength.GetValue(sequence);
            }
            else
            {
                var piCount = sequenceType.GetProperty(nameof(ICollection.Count), BindingFlags.Instance | BindingFlags.Public);

                if (piCount != null)
                {
                    count = (int)piCount.GetValue(sequence);
                }
                else
                {
                    count = int.MaxValue;
                }
            }

            // how many items to dump max?
            var max = dumpAttribute.GetMaxToDump(count);

            writer.Write(
                DumpFormat.SequenceTypeName,
                sequenceType.IsArray
                        ? elementsType[0].GetTypeName()
                        : sequenceType.GetTypeName(),
                count > 0 &&
                count < int.MaxValue
                        ? count.ToString(CultureInfo.InvariantCulture)
                        : string.Empty);

            if (sequence is byte[] bytes)
            {
                // dump no more than max elements from the sequence:
                writer.Write(BitConverter.ToString(bytes, 0, max));
                if (max < bytes.Length)
                {
                    writer.Write(DumpFormat.SequenceDumpTruncated, max, count);
                }

                return(true);
            }

            writer.Write(
                DumpFormat.SequenceType,
                sequenceType.GetTypeName(),
                sequenceType.Namespace,
                sequenceType.AssemblyQualifiedName);

            // stop the recursion if dump.Recurse is false
            if (dumpAttribute.RecurseDump != ShouldDump.Skip)
            {
                var n = 0;

                indent();

                foreach (var item in sequence)
                {
                    writer.WriteLine();
                    if (n++ >= max)
                    {
                        writer.Write(DumpFormat.SequenceDumpTruncated, max, count);
                        break;
                    }
                    dumpObject(item);
                }

                unindent();
            }

            return(true);
        }
Ejemplo n.º 2
0
        public static bool DumpedDictionary(
            this TextWriter writer,
            object sequence,
            DumpAttribute dumpAttribute,
            Action <object> dumpObject,
            Action indent,
            Action unindent)
        {
            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }
            if (sequence == null)
            {
                throw new ArgumentNullException(nameof(sequence));
            }
            if (dumpAttribute == null)
            {
                throw new ArgumentNullException(nameof(dumpAttribute));
            }
            if (dumpObject == null)
            {
                throw new ArgumentNullException(nameof(dumpObject));
            }
            if (indent == null)
            {
                throw new ArgumentNullException(nameof(indent));
            }
            if (unindent == null)
            {
                throw new ArgumentNullException(nameof(unindent));
            }

            var sequenceType  = sequence.GetType();
            var typeArguments = sequenceType.DictionaryTypeArguments();

            if (typeArguments == null)
            {
                return(false);
            }

            Debug.Assert(typeArguments.Length == 2);

            var keyType   = typeArguments[0];
            var valueType = typeArguments[1];

            int count   = 0;
            var piCount = sequenceType.GetProperty(nameof(ICollection.Count), BindingFlags.Instance | BindingFlags.Public);

            if (piCount != null)
            {
                count = (int)piCount.GetValue(sequence);
            }

            var keyValueType = typeof(KeyValuePair <,>).MakeGenericType(keyType, valueType);

            writer.Write(
                DumpFormat.SequenceTypeName,
                sequenceType.GetTypeName(),
                count.ToString(CultureInfo.InvariantCulture));

            writer.Write(
                DumpFormat.SequenceType,
                sequenceType.GetTypeName(),
                sequenceType.Namespace,
                sequenceType.AssemblyQualifiedName);

            // stop the recursion if dump.Recurse is false
            if (dumpAttribute.RecurseDump == ShouldDump.Skip)
            {
                return(true);
            }

            // how many items to dump max?
            var max = dumpAttribute.GetMaxToDump(count);
            var n   = 0;

            writer.WriteLine();
            writer.Write("{");
            indent();

            foreach (var kv in (IEnumerable)sequence)
            {
                Debug.Assert(kv.GetType() == keyValueType);

                writer.WriteLine();
                if (n++ >= max)
                {
                    writer.Write(DumpFormat.SequenceDumpTruncated, max, count);
                    break;
                }

                var key   = keyValueType.GetProperty("Key").GetValue(kv, null);
                var value = keyValueType.GetProperty("Value").GetValue(kv, null);

                writer.Write("[");
                dumpObject(key);
                writer.Write("] = ");

                dumpObject(value);
            }

            unindent();
            writer.WriteLine();
            writer.Write("}");

            return(true);
        }