示例#1
0
        // caller is responsible for ensuring constant is on the right
        public static AstFilter Translate(
            TranslationContext context,
            Expression expression,
            Expression leftExpression,
            AstComparisonFilterOperator comparisonOperator,
            Expression rightExpression)
        {
            if (leftExpression is BinaryExpression leftBinaryExpression &&
                leftBinaryExpression.NodeType == ExpressionType.And)
            {
                var fieldExpression = leftBinaryExpression.Left;
                var field           = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression);

                var bitMaskExpression = leftBinaryExpression.Right;
                var bitMask           = bitMaskExpression.GetConstantValue <object>(containingExpression: expression);
                var serializedBitMask = SerializationHelper.SerializeValue(field.Serializer, bitMask);

                var rightValue = rightExpression.GetConstantValue <object>(containingExpression: expression);
                var zeroValue  = Activator.CreateInstance(bitMask.GetType());

                switch (comparisonOperator)
                {
                case AstComparisonFilterOperator.Eq:
                    if (rightValue.Equals(zeroValue))
                    {
                        return(AstFilter.BitsAllClear(field, serializedBitMask));
                    }
                    else if (rightValue.Equals(bitMask))
                    {
                        return(AstFilter.BitsAllSet(field, serializedBitMask));
                    }
                    break;

                case AstComparisonFilterOperator.Ne:
                    if (rightValue.Equals(zeroValue))
                    {
                        return(AstFilter.BitsAnySet(field, serializedBitMask));
                    }
                    else if (rightValue.Equals(bitMask))
                    {
                        return(AstFilter.BitsAnyClear(field, serializedBitMask));
                    }
                    break;
                }
            }

            throw new ExpressionNotSupportedException(expression);
        }
        // public static methods
        public static AstFilter Translate(TranslationContext context, MethodCallExpression expression)
        {
            var method    = expression.Method;
            var arguments = expression.Arguments;

            if (method.Is(EnumMethod.HasFlag))
            {
                var fieldExpression = expression.Object;
                var field           = ExpressionToFilterFieldTranslator.Translate(context, fieldExpression);

                var flagExpression = arguments[0];
                var flag           = flagExpression.GetConstantValue <object>(containingExpression: expression);
                var serializedFlag = SerializationHelper.SerializeValue(field.Serializer, flag);
                return(AstFilter.BitsAllSet(field, serializedFlag));
            }

            throw new ExpressionNotSupportedException(expression);
        }