コード例 #1
0
        private static bool TryTranslateFromTypeCode(
            ConstantExpression constant,
            ITranslationContext context,
            out ITranslation translation)
        {
            var type = constant.Type;

            switch ((Nullable.GetUnderlyingType(type) ?? type).GetTypeCode())
            {
            case NetStandardTypeCode.Boolean:
                translation = FixedValueTranslation(
                    constant.Value.ToString().ToLowerInvariant(),
                    type,
                    Keyword,
                    context);

                return(true);

            case NetStandardTypeCode.Char:
                var character = (char)constant.Value;
                var value     = "'" + (character == '\0' ? @"\0" : character.ToString()) + "'";
                translation = FixedValueTranslation(value, type, Text, context);
                return(true);

            case NetStandardTypeCode.DateTime:
                if (!TryTranslateDefault <DateTime>(constant, context, out translation))
                {
                    translation = new DateTimeConstantTranslation(constant, context);
                }

                return(true);

            case NetStandardTypeCode.DBNull:
                translation = new DbNullTranslation(constant, context);
                return(true);

            case NetStandardTypeCode.Decimal:
                translation = GetDecimalTranslation(constant, context);
                return(true);

            case NetStandardTypeCode.Double:
                translation = GetDoubleTranslation(constant, context);
                return(true);

            case NetStandardTypeCode.Int64:
                translation = GetLongTranslation(constant, context);
                return(true);

            case NetStandardTypeCode.Int32:
                translation = FixedValueTranslation(constant, context, Numeric);
                return(true);

            case NetStandardTypeCode.Object:
                if (TryGetTypeTranslation(constant, context, out translation) ||
                    LambdaConstantTranslation.TryCreate(constant, context, out translation) ||
                    TryGetRegexTranslation(constant, context, out translation) ||
                    TryTranslateDefault <Guid>(constant, context, out translation) ||
                    TimeSpanConstantTranslation.TryCreate(constant, context, out translation))
                {
                    return(true);
                }

                break;

            case NetStandardTypeCode.Single:
                translation = GetFloatTranslation(constant, context);
                return(true);

            case NetStandardTypeCode.String:
                var stringValue = (string)constant.Value;

                if (stringValue.IsComment())
                {
                    translation = new CommentTranslation(stringValue.Replace("\0", @"\0"), context);
                    return(true);
                }

                stringValue = "\"" + stringValue.Replace(@"\", @"\\").Replace("\0", @"\0").Replace(@"""", @"\""") + "\"";
                translation = FixedValueTranslation(stringValue, typeof(string), Text, context);
                return(true);
            }

            translation = null;
            return(false);
        }
コード例 #2
0
        private static bool TryTranslateFromTypeCode(
            ConstantExpression constant,
            ITranslationContext context,
            out ITranslation translation)
        {
            switch ((Nullable.GetUnderlyingType(constant.Type) ?? constant.Type).GetTypeCode())
            {
            case NetStandardTypeCode.Boolean:
                translation = FixedValueTranslation(constant.Value.ToString().ToLowerInvariant(), constant.Type);
                return(true);

            case NetStandardTypeCode.Char:
                var character = (char)constant.Value;
                var value     = character == '\0' ? Expression.Constant(@"\0") : constant;
                translation = new TranslationWrapper(FixedValueTranslation(value)).WrappedWith("'", "'");
                return(true);

            case NetStandardTypeCode.DateTime:
                if (!TryTranslateDefault <DateTime>(constant, out translation))
                {
                    translation = new DateTimeConstantTranslation(constant);
                }

                return(true);

            case NetStandardTypeCode.DBNull:
                translation = FixedValueTranslation("DBNull.Value", constant.Type);
                return(true);

            case NetStandardTypeCode.Decimal:
                translation = GetDecimalTranslation(constant);
                return(true);

            case NetStandardTypeCode.Double:
                translation = GetDoubleTranslation(constant);
                return(true);

            case NetStandardTypeCode.Int64:
                translation = new TranslationWrapper(FixedValueTranslation(constant)).WithSuffix("L");
                return(true);

            case NetStandardTypeCode.Int32:
                translation = FixedValueTranslation(constant);
                return(true);

            case NetStandardTypeCode.Object:
                if (TryGetTypeTranslation(constant, context, out translation) ||
                    LambdaConstantTranslation.TryCreate(constant, context, out translation) ||
                    TryGetRegexTranslation(constant, out translation) ||
                    TryTranslateDefault <Guid>(constant, out translation) ||
                    TimeSpanConstantTranslation.TryCreate(constant, out translation))
                {
                    return(true);
                }

                break;

            case NetStandardTypeCode.Single:
                translation = GetFloatTranslation(constant);
                return(true);

            case NetStandardTypeCode.String:
                var stringValue = ((string)constant.Value).Replace("\0", @"\0");

                if (stringValue.IsComment())
                {
                    translation = FixedValueTranslation(stringValue, typeof(string), isTerminated: true);
                    return(true);
                }

                translation = FixedValueTranslation(stringValue.Replace("\"", "\\\""), typeof(string));
                translation = new TranslationWrapper(translation).WrappedWith("\"", "\"");

                return(true);
            }

            translation = null;
            return(false);
        }