Пример #1
0
        private static XElement ToXElementInternal(object o, ICollection <object> visited, BindingFlags flags, int maxArrayElements)
        {
            if (o == null)
            {
                return(new XElement((XName)"null"));
            }
            if (visited.Contains(o))
            {
                return(new XElement((XName)"cyclicreference"));
            }
            if (!o.GetType().IsValueType)
            {
                visited.Add(o);
            }
            Type     type1    = o.GetType();
            XElement xelement = new XElement((XName)ObjectExtensions.CleanName((IEnumerable <char>)type1.Name, type1.IsArray));

            if (!ObjectExtensions.NeedRecursion(type1, o))
            {
                xelement.Add((object)new XElement((XName)ObjectExtensions.CleanName((IEnumerable <char>)type1.Name, type1.IsArray), (object)(string.Empty + o)));
                return(xelement);
            }
            if (o is IEnumerable)
            {
                int num = 0;
                foreach (object obj in o as IEnumerable)
                {
                    Type type2 = obj.GetType();
                    xelement.Add(ObjectExtensions.NeedRecursion(type2, obj) ? (object)ObjectExtensions.ToXElementInternal(obj, visited, flags, maxArrayElements) : (object)new XElement((XName)ObjectExtensions.CleanName((IEnumerable <char>)type2.Name, type2.IsArray), obj));
                    if (num++ >= maxArrayElements)
                    {
                        break;
                    }
                }
                return(xelement);
            }
            foreach (PropertyInfo propertyInfo in ((IEnumerable <PropertyInfo>)type1.GetProperties(flags)).Where <PropertyInfo>((Func <PropertyInfo, bool>)(propertyInfo => propertyInfo.CanRead)))
            {
                object o1 = ObjectExtensions.GetValue(o, propertyInfo);
                xelement.Add(ObjectExtensions.NeedRecursion(propertyInfo.PropertyType, o1) ? (object)new XElement((XName)ObjectExtensions.CleanName((IEnumerable <char>)propertyInfo.Name, propertyInfo.PropertyType.IsArray), (object)ObjectExtensions.ToXElementInternal(o1, visited, flags, maxArrayElements)) : (object)new XElement((XName)ObjectExtensions.CleanName((IEnumerable <char>)propertyInfo.Name, propertyInfo.PropertyType.IsArray), (object)(string.Empty + o1)));
            }
            foreach (FieldInfo field in type1.GetFields())
            {
                object o1 = field.GetValue(o);
                xelement.Add(ObjectExtensions.NeedRecursion(field.FieldType, o1) ? (object)new XElement((XName)ObjectExtensions.CleanName((IEnumerable <char>)field.Name, field.FieldType.IsArray), (object)ObjectExtensions.ToXElementInternal(o1, visited, flags, maxArrayElements)) : (object)new XElement((XName)ObjectExtensions.CleanName((IEnumerable <char>)field.Name, field.FieldType.IsArray), (object)(string.Empty + o1)));
            }
            return(xelement);
        }
Пример #2
0
 public static string ToHTMLTable(this object o, BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, int maxArrayElements = 5)
 {
     return(ObjectExtensions.ToHTMLTableInternal((XContainer)o.ToXElement(flags, maxArrayElements), 0).Aggregate <string, string>(string.Empty, (Func <string, string, string>)((str, el) => str + el)));
 }