コード例 #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));
        }
コード例 #2
0
        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());
            }
        }