Esempio n. 1
0
        public virtual string FormatTypeName(Type type)
        {
            if ((object)type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            string primitiveTypeName = this.GetPrimitiveTypeName(ObjectFormatterHelpers.GetPrimitiveSpecialType(type));

            if (primitiveTypeName != null)
            {
                return(primitiveTypeName);
            }
            if (type.IsGenericParameter)
            {
                return(type.Name);
            }
            if (type.IsArray)
            {
                return(this.FormatArrayTypeName(type, null));
            }
            System.Reflection.TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(type);
            if (typeInfo.IsGenericType)
            {
                return(this.FormatGenericTypeName(typeInfo));
            }
            return(CommonTypeNameFormatter.FormatNonGenericTypeName(typeInfo));
        }
        protected override string FormatLiteral(
            string value,
            bool useQuotes,
            bool escapeNonPrintable)
        {
            ObjectDisplayOptions objectDisplayOptions = ObjectFormatterHelpers.GetObjectDisplayOptions(useQuotes, escapeNonPrintable, false);

            return(ObjectDisplay.FormatLiteral(value, objectDisplayOptions));
        }
        protected override string FormatLiteral(
            char c,
            bool useQuotes,
            bool escapeNonPrintable,
            bool includeCodePoints = false)
        {
            ObjectDisplayOptions objectDisplayOptions = ObjectFormatterHelpers.GetObjectDisplayOptions(useQuotes, escapeNonPrintable, includeCodePoints);

            return(ObjectDisplay.FormatLiteral(c, objectDisplayOptions));
        }
        public string FormatPrimitive(object obj, CommonPrimitiveFormatterOptions options)
        {
            if (obj == ObjectFormatterHelpers.VoidValue)
            {
                return(string.Empty);
            }
            if (obj == null)
            {
                return(this.NullLiteral);
            }
            Type type = obj.GetType();

            if (IntrospectionExtensions.GetTypeInfo(type).IsEnum)
            {
                return(obj.ToString());
            }
            switch (ObjectFormatterHelpers.GetPrimitiveSpecialType(type))
            {
            case SpecialType.None:
            case SpecialType.System_Object:
            case SpecialType.System_Void:
                return(null);

            case SpecialType.System_Boolean:
                return(this.FormatLiteral((bool)obj));

            case SpecialType.System_Char:
                return(this.FormatLiteral((char)obj, options.QuoteStringsAndCharacters, options.EscapeNonPrintableCharacters, options.IncludeCharacterCodePoints));

            case SpecialType.System_SByte:
                return(this.FormatLiteral((sbyte)obj, options.CultureInfo));

            case SpecialType.System_Byte:
                return(this.FormatLiteral((byte)obj, options.CultureInfo));

            case SpecialType.System_Int16:
                return(this.FormatLiteral((short)obj, options.CultureInfo));

            case SpecialType.System_UInt16:
                return(this.FormatLiteral((ushort)obj, options.CultureInfo));

            case SpecialType.System_Int32:
                return(this.FormatLiteral((int)obj, options.CultureInfo));

            case SpecialType.System_UInt32:
                return(this.FormatLiteral((uint)obj, options.CultureInfo));

            case SpecialType.System_Int64:
                return(this.FormatLiteral((long)obj, options.CultureInfo));

            case SpecialType.System_UInt64:
                return(this.FormatLiteral((ulong)obj, options.CultureInfo));

            case SpecialType.System_Decimal:
                return(this.FormatLiteral((Decimal)obj, options.CultureInfo));

            case SpecialType.System_Single:
                return(this.FormatLiteral((float)obj, options.CultureInfo));

            case SpecialType.System_Double:
                return(this.FormatLiteral((double)obj, options.CultureInfo));

            case SpecialType.System_String:
                return(this.FormatLiteral((string)obj, options.QuoteStringsAndCharacters, options.EscapeNonPrintableCharacters));

            case SpecialType.System_DateTime:
                return(this.FormatLiteral((DateTime)obj, options.CultureInfo));

            default:
                throw new NotSupportedException(ObjectFormatterHelpers.GetPrimitiveSpecialType(type).ToString());
            }
        }
            private void FormatObjectMembersRecursive(
                List <CommonObjectFormatter.Visitor.FormattedMember> result,
                object obj)
            {
                List <MemberInfo> memberInfoList = new List <MemberInfo>();
                Type baseType;

                for (System.Reflection.TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(obj.GetType());
                     typeInfo != null;
                     typeInfo = (object)baseType != null ? IntrospectionExtensions.GetTypeInfo(baseType) : null)
                {
                    memberInfoList.AddRange(Enumerable.Where <FieldInfo>(typeInfo.DeclaredFields, f => !f.IsStatic));
                    memberInfoList.AddRange(Enumerable.Where <PropertyInfo>(typeInfo.DeclaredProperties, f =>
                    {
                        if ((object)f.GetMethod != null)
                        {
                            return(!f.GetMethod.IsStatic);
                        }
                        return(false);
                    }));
                    baseType = typeInfo.BaseType;
                }
                memberInfoList.Sort((x, y) =>
                {
                    int num = StringComparer.OrdinalIgnoreCase.Compare(x.Name, y.Name);
                    if (num == 0)
                    {
                        num = StringComparer.Ordinal.Compare(x.Name, y.Name);
                    }
                    return(num);
                });
                foreach (MemberInfo member in memberInfoList)
                {
                    bool flag1 = false;
                    bool flag2 = false;
                    if (member is FieldInfo fieldInfo)
                    {
                        if (!(flag2) && !fieldInfo.IsPublic && (!fieldInfo.IsFamily && !fieldInfo.IsFamilyOrAssembly))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        PropertyInfo propertyInfo = (PropertyInfo)member;
                        MethodInfo   getMethod    = propertyInfo.GetMethod;
                        if ((object)getMethod != null)
                        {
                            MethodInfo setMethod = propertyInfo.SetMethod;
                            if (!(flag2) && !getMethod.IsPublic && (!getMethod.IsFamily && !getMethod.IsFamilyOrAssembly) && ((object)setMethod == null || !setMethod.IsPublic && !setMethod.IsFamily && !setMethod.IsFamilyOrAssembly) || getMethod.GetParameters().Length != 0)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
                    }
                    Exception exception;
                    object    memberValue = ObjectFormatterHelpers.GetMemberValue(member, obj, out exception);
                    if (exception != null)
                    {
                        CommonObjectFormatter.Builder result1 = new Builder();
                        this.FormatException(result1, exception);
                        if (!this.AddMember(result, new CommonObjectFormatter.Visitor.FormattedMember(-1, member.Name, result1.ToString())))
                        {
                            break;
                        }
                    }
                    else if (flag1)
                    {
                        if (memberValue != null && !this.VisitedObjects.Contains(memberValue))
                        {
                            if (memberValue is Array array)
                            {
                                int index = 0;
                                foreach (object obj1 in array)
                                {
                                    CommonObjectFormatter.Builder result1 = new Builder();
                                    this.FormatObjectRecursive(result1, obj1, false);
                                    if (!this.AddMember(result, new CommonObjectFormatter.Visitor.FormattedMember(index, "", result1.ToString())))
                                    {
                                        return;
                                    }
                                    ++index;
                                }
                            }
                            else if (this._formatter.PrimitiveFormatter.FormatPrimitive(memberValue, this._primitiveOptions) == null && this.VisitedObjects.Add(memberValue))
                            {
                                this.FormatObjectMembersRecursive(result, memberValue);
                                this.VisitedObjects.Remove(memberValue);
                            }
                        }
                    }
                    else
                    {
                        CommonObjectFormatter.Builder result1 = new Builder();
                        this.FormatObjectRecursive(result1, memberValue, false);
                        if (!this.AddMember(result, new CommonObjectFormatter.Visitor.FormattedMember(-1, "", result1.ToString())))
                        {
                            break;
                        }
                    }
                }
            }
            private CommonObjectFormatter.Builder FormatObjectRecursive(
                CommonObjectFormatter.Builder result,
                object obj,
                bool isRoot)
            {
                string str = this._formatter.PrimitiveFormatter.FormatPrimitive(obj, this._primitiveOptions);

                if (str != null)
                {
                    result.Append(str, 0, int.MaxValue);
                    return(result);
                }
                Type type = obj.GetType();

                System.Reflection.TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(type);
                if (typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == (object)typeof(KeyValuePair <,>))
                {
                    if (isRoot)
                    {
                        result.Append(this._formatter.TypeNameFormatter.FormatTypeName(type), 0, int.MaxValue);
                        result.Append(' ');
                    }
                    this.FormatKeyValuePair(result, obj);
                    return(result);
                }
                if (typeInfo.IsArray)
                {
                    if (this.VisitedObjects.Add(obj))
                    {
                        this.FormatArray(result, (Array)obj);
                        this.VisitedObjects.Remove(obj);
                    }
                    else
                    {
                        result.AppendInfiniteRecursionMarker();
                    }
                    return(result);
                }
                bool        flag       = false;
                ICollection collection = null;

                if (obj is ICollection)
                {
                    collection = (ICollection)obj;
                    this.FormatCollectionHeader(result, collection);
                }
                else if (ObjectFormatterHelpers.HasOverriddenToString(typeInfo))
                {
                    this.ObjectToString(result, obj);
                    flag = true;
                }
                else
                {
                    result.Append(this._formatter.TypeNameFormatter.FormatTypeName(type), 0, int.MaxValue);
                }

                if (!flag)
                {
                    this.FormatMembers(result, obj);
                }
                return(result);
            }
 protected override string FormatLiteral(ulong value, CultureInfo cultureInfo = null)
 {
     return(ObjectDisplay.FormatLiteral(value, ObjectFormatterHelpers.GetObjectDisplayOptions(false, false, false), cultureInfo));
 }