/// <summary>
        /// Returns the System.String that represents the current TypedConstant.
        /// </summary>
        /// <returns>A System.String that represents the current TypedConstant.</returns>
        public static string ToCSharpString(this TypedConstant constant)
        {
            if (constant.IsNull)
            {
                return("null");
            }

            if (constant.Kind == TypedConstantKind.Array)
            {
                return("{" + string.Join(", ", constant.Values.Select(v => v.ToCSharpString())) + "}");
            }

            if (constant.Kind == TypedConstantKind.Type || constant.Type.SpecialType == SpecialType.System_Object)
            {
                return("typeof(" + constant.Value.ToString() + ")");
            }

            if (constant.Kind == TypedConstantKind.Enum)
            {
                // TODO (tomat): replace with SymbolDisplay
                return(DisplayEnumConstant(constant));
            }

            return(SymbolDisplay.FormatPrimitive(constant.Value, quoteStrings: true, useHexadecimalNumbers: false));
        }
Example #2
0
        protected override void AddLiteralValue(SpecialType type, object value)
        {
            Debug.Assert(value.GetType().GetTypeInfo().IsPrimitive || value is string || value is decimal);
            var valueString = SymbolDisplay.FormatPrimitive(value, quoteStrings: true, useHexadecimalNumbers: false);

            Debug.Assert(valueString != null);

            var kind = SymbolDisplayPartKind.NumericLiteral;

            switch (type)
            {
            case SpecialType.System_Boolean:
                kind = SymbolDisplayPartKind.Keyword;
                break;

            case SpecialType.System_String:
            case SpecialType.System_Rune:
                kind = SymbolDisplayPartKind.StringLiteral;
                break;
            }

            this.builder.Add(CreatePart(kind, null, valueString));
        }