Пример #1
0
        private static void DumpEnumerable(IEnumerable enumerable, DumpContext ctx)
        {
            Int32        nCount          = 0;
            Boolean      isSimpleType    = true;
            Boolean      isKeyValuePairs = false;
            PropertyInfo keyProp         = null;
            PropertyInfo valueProp       = null;
            var          type            = (from object value in enumerable
                                            where value != null
                                            select value.GetType()).FirstOrDefault();

            if (type != null)
            {
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
                {
                    keyProp         = type.GetProperty("Key");
                    valueProp       = type.GetProperty("Value");
                    isKeyValuePairs = true;
                }
                isSimpleType = Type.GetTypeCode(type) != TypeCode.Object;
            }
            foreach (Object value in enumerable)
            {
                if (!isSimpleType)
                {
                    ctx.NewLine();
                }
                else if (nCount > 0)
                {
                    ctx.Builder.Append(ctx.Settings.EnumerableDelimiter);
                }
                if (nCount >= ctx.Settings.MaxEnumerableItems)
                {
                    ctx.Builder.Append("... (first ").Append(nCount.ToString()).Append(" items");
                    var collection = enumerable as ICollection;
                    if (collection != null)
                    {
                        ctx.Builder.Append(", ").Append(collection.Count.ToString()).Append(" items total");
                    }
                    ctx.Builder.Append(")");
                    break;
                }
                if (!isKeyValuePairs)
                {
                    DumpObject(value, true, ctx);
                }
                else
                {
                    ctx.Builder.Append(ToStringSafe(keyProp.GetValue(value, null)));
                    ctx.Builder.Append(": ");
                    DumpObject(valueProp.GetValue(value, null), false, ctx);
                }
                nCount++;
            }
            if (nCount == 0)
            {
                ctx.Builder.Append("<EMPTY>");
            }
        }
Пример #2
0
        private static void DumpProps(object obj, Type type, DumpContext ctx)
        {
            var propValues = new Dictionary <String, Object>();

            PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (var prop in props)
            {
                if (!prop.CanRead || prop.GetIndexParameters().Length != 0)
                {
                    continue;
                }
                if (ctx.Settings.PropsToIgnore.Contains(prop.Name))
                {
                    continue;
                }
                try
                {
                    propValues[prop.Name] = prop.GetValue(obj, null);
                }
                catch (TargetInvocationException ex)
                {
                    propValues[prop.Name] = String.Format("Ошибка при получении свойства {0} объекта типа {1} : {2}",
                                                          prop.Name,
                                                          type.FullName,
                                                          ex.InnerException != null ? ex.InnerException.Message : ex.Message
                                                          );
                }
            }
            FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
            foreach (var field in fields)
            {
                if (ctx.Settings.PropsToIgnore.Contains(field.Name))
                {
                    continue;
                }
                try
                {
                    propValues[field.Name] = field.GetValue(obj);
                }
                catch (FieldAccessException ex)
                {
                    propValues[field.Name] = ex.Message;
                }
            }
            if (propValues.Count == 0 || propValues.Count > ctx.Settings.MaxProps)
            {
                WriteObject(obj, type, ctx);
            }
            else
            {
                foreach (KeyValuePair <String, Object> pair in propValues)
                {
                    ctx.NewLine();
                    ctx.Builder.Append(pair.Key).Append(": ");
                    DumpObject(pair.Value, false, ctx);
                }
            }
        }
Пример #3
0
 private static void DumpEnumerable(IEnumerable enumerable, DumpContext ctx)
 {
     Int32 nCount = 0;
     Boolean isSimpleType = true;
     Boolean isKeyValuePairs = false;
     PropertyInfo keyProp = null;
     PropertyInfo valueProp = null;
     var type = (from object value in enumerable
                 where value != null
                 select value.GetType()).FirstOrDefault();
     if (type != null)
     {
         if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof (KeyValuePair<,>))
         {
             keyProp = type.GetProperty("Key");
             valueProp = type.GetProperty("Value");
             isKeyValuePairs = true;
         }
         isSimpleType = Type.GetTypeCode(type) != TypeCode.Object;
     }
     foreach (Object value in enumerable)
     {
         if (!isSimpleType)
             ctx.NewLine();
         else if (nCount > 0)
             ctx.Builder.Append(ctx.Settings.EnumerableDelimiter);
         if (nCount >= ctx.Settings.MaxEnumerableItems)
         {
             ctx.Builder.Append("... (first ").Append(nCount.ToString()).Append(" items");
             var collection = enumerable as ICollection;
             if (collection != null)
                 ctx.Builder.Append(", ").Append(collection.Count.ToString()).Append(" items total");
             ctx.Builder.Append(")");
             break;
         }
         if (!isKeyValuePairs)
             DumpObject(value, true, ctx);
         else
         {
             ctx.Builder.Append(ToStringSafe(keyProp.GetValue(value, null)));
             ctx.Builder.Append(": ");
             DumpObject(valueProp.GetValue(value, null), false, ctx);
         }
         nCount++;
     }
     if (nCount == 0)
         ctx.Builder.Append("<EMPTY>");
 }
Пример #4
0
 private static void DumpProps(object obj, Type type, DumpContext ctx)
 {
     var propValues = new Dictionary<String, Object>();
     PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
     foreach (var prop in props)
     {
         if (!prop.CanRead || prop.GetIndexParameters().Length != 0)
             continue;
         if (ctx.Settings.PropsToIgnore.Contains(prop.Name))
             continue;
         try
         {
             propValues[prop.Name] = prop.GetValue(obj, null);
         }
         catch (TargetInvocationException ex)
         {
             propValues[prop.Name] = String.Format("Ошибка при получении свойства {0} объекта типа {1} : {2}",
                 prop.Name,
                 type.FullName,
                 ex.InnerException != null ? ex.InnerException.Message : ex.Message
                 );
         }
     }
     FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
     foreach (var field in fields)
     {
         if (ctx.Settings.PropsToIgnore.Contains(field.Name))
             continue;
         try
         {
             propValues[field.Name] = field.GetValue(obj);
         }
         catch (FieldAccessException ex)
         {
             propValues[field.Name] = ex.Message;
         }
     }
     if (propValues.Count == 0 || propValues.Count > ctx.Settings.MaxProps)
         WriteObject(obj, type, ctx);
     else
     {
         foreach (KeyValuePair<String, Object> pair in propValues)
         {
             ctx.NewLine();
             ctx.Builder.Append(pair.Key).Append(": ");
             DumpObject(pair.Value, false, ctx);
         }
     }
 }