Example #1
0
        private static string NonGenericDictionaryToString(object dicInstance, string propName, int layer)
        {
            StringBuilder sb = new StringBuilder();
            if (dicInstance == null)
            {
                if (String.IsNullOrEmpty(propName))
                    sb.AppendLayerFormatLine(layer, "[null]");
                else
                    sb.AppendLayerFormatLine(layer, "{0}: [null]", propName);
            }
            else
            {
                if (!String.IsNullOrEmpty(propName))
                    sb.AppendLayerFormatLine(layer, "{0}:", propName);

                foreach (object pair in dicInstance as IEnumerable)
                {
                    if (pair == null)
                    {
                        sb.AppendLayerFormatLine(layer + 1, "[null]");
                    }
                    else
                    {
                        sb.AppendLayerFormatLine(layer + 1, "[");

                        object objKey = pair.GetType().GetProperty("Key").GetValue(pair, null);
                        object objValue = pair.GetType().GetProperty("Value").GetValue(pair, null);

                        if (objKey == null || IsBasicType(objKey.GetType()) )
                        {
                            sb.AppendLayerFormatLine(layer + 1, "Key: {0}",
                                objKey == null ? "[null]" : objKey.ToString()
                            );
                        }
                        else
                        {
                            sb.AppendLayerFormatLine(layer + 1, "Key: ");
                            sb.AppendLayerFormatLine(layer + 2, "[");
                            sb.Append(GeneralToString(objKey, layer + 3));
                            sb.AppendLayerFormatLine(layer + 2, "]");
                        }

                        if (objValue == null || IsBasicType(objValue.GetType()))
                        {
                            sb.AppendLayerFormatLine(layer + 1, "Value: {0}",
                                objValue == null ? "[null]" : objValue.ToString()
                            );
                        }
                        else
                        {
                            sb.AppendLayerFormatLine(layer + 1, "Value: ");
                            sb.AppendLayerFormatLine(layer + 2, "[");
                            sb.Append(GeneralToString(objValue, layer + 3));
                            sb.AppendLayerFormatLine(layer + 2, "]");
                        }

                        sb.AppendLayerFormatLine(layer + 1, "]");
                    }
                }
            }

            return sb.ToString();
        }
Example #2
0
        private static string CollectionToString(object collectionInstance, string propName, int layer)
        {
            //object collectionInstance = prop.GetValue(o, null);
            StringBuilder sb = new StringBuilder();
            if (collectionInstance == null)
            {
                if(String.IsNullOrEmpty(propName))
                    sb.AppendLayerFormatLine(layer, "[null]");
                else
                    sb.AppendLayerFormatLine(layer, "{0}: [null]", propName);
            }
            else
            {
                if (!String.IsNullOrEmpty(propName))
                {
                    string strSize = "";
                    if (collectionInstance.GetType().IsArray)
                    {
                        System.Array ar = collectionInstance as System.Array;
                        int rank = ar.Rank;
                        StringBuilder ars = new StringBuilder();
                        for (int i = 0; i < rank; i++)
                        {
                            if (i != 0)
                                ars.Append("*");
                            ars.Append(ar.GetLength(i));
                        }
                        strSize = String.Format("[size: {0}]", ars.ToString());
                    }

                    sb.AppendLayerFormatLine(layer, "{0}: {1}", propName, strSize);
                }

                foreach (object item in collectionInstance as IEnumerable)
                {
                    if (IsBasicType(item.GetType()) || item == null)
                    {
                        sb.AppendLayerFormatLine(layer + 1, "[{0}]", item == null ? "null" : item.ToString());
                    }
                    else
                    {
                        sb.AppendLayerFormatLine(layer + 1, "[");
                        sb.Append(GeneralToString(item, layer + 2));
                        sb.AppendLayerFormatLine(layer + 1, "]");
                    }
                }
            }
            return sb.ToString();
        }
Example #3
0
        private static string GeneralToString(object o, int layer)
        {
            StringBuilder sb = new StringBuilder();
            if (o == null)
            {
                sb.AppendLayerFormatLine(layer, "[null]");
                //sb.AppendLine("[null]");
            }
            else if (IsBasicType(o.GetType()))
            {
                sb.AppendLayerFormatLine(layer, o.ToString());
            }
            else if (IsDictionary(o.GetType()))
            {
                sb.Append(DictionaryToString(o, null, layer));
            }
            else if (IsCollection(o.GetType()))
            {
                sb.Append(CollectionToString(o, null, layer));
            }
            else if (IsIFormattable(o.GetType()))
            {
                sb.AppendLayerFormatLine(layer, o.ToString());
            }
            else
            {
                Type propType;
                foreach (var prop in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    if (!(prop.CanRead))
                        continue;

                    if(prop.GetIndexParameters().Length > 0) // do not print indexers
                        continue;

                    if (ReflectionUtils.IsTypeEqualOrInheritedFromType(prop.PropertyType, typeof(Delegate)))
                        continue; // do not print delegates

                    propType = prop.PropertyType;
                    if (IsBasicType(propType))
                    {
                        sb.AppendLayerFormatLine(layer, "{0}: {1}", prop.Name, GetBasicPropertyValue(o, prop));
                    }
                    else if (IsDictionary(propType))
                    {
                        sb.Append(DictionaryToString(prop.GetValue(o, null), prop.Name, layer));
                    }
                    else if (IsCollection(propType))
                    {
                        sb.Append(CollectionToString(prop.GetValue(o, null), prop.Name, layer));
                    }
                    else if (IsNonGenericIDictionary(propType))
                    {
                        sb.Append(NonGenericDictionaryToString(prop.GetValue(o, null), prop.Name, layer));
                    }
                    else if (IsNonGenericIEnumerable(propType))
                    {
                        sb.Append(CollectionToString(prop.GetValue(o, null), prop.Name, layer));
                    }
                    else
                    {
                        object propValue = prop.GetValue(o, null);
                        if (propValue == null)
                        {
                            sb.AppendLayerFormatLine(layer, "{0}: [null]", prop.Name);
                        }
                        else
                        {
                            sb.AppendLayerFormatLine(layer, "{0}:", prop.Name);

                            sb.AppendLayerFormatLine(layer, "[");
                            sb.Append(GeneralToString(propValue, layer + 1));
                            sb.AppendLayerFormatLine(layer, "]");
                        }
                    }
                }
            }

            return sb.ToString();
        }