public SerializableExpression TryConvert(Expression expression)
 {
     if (expression == null)
     {
         return(null);
     }
     else if (serialized.ContainsKey(expression))
     {
         /* Caching is required to maintain object references during serialization.
          * See the comments on SerializableExpression.ConvertWithCache for more info.
          */
         return(serialized[expression]);
     }
     else if ((methodCall = expression as MethodCallExpression) != null)
     {
         return(serialized[expression] = new SerializableMethodCallExpression(methodCall, this));
     }
     else if ((lambda = expression as LambdaExpression) != null)
     {
         return(serialized[expression] = new SerializableLambdaExpression(lambda, this));
     }
     else if ((constant = expression as ConstantExpression) != null)
     {
         return(serialized[expression] = new SerializableConstantExpression(constant));
     }
     else if ((member = expression as MemberExpression) != null)
     {
         return(serialized[expression] = new SerializableMemberExpression(member, this));
     }
     else if ((binary = expression as BinaryExpression) != null)
     {
         return(serialized[expression] = new SerializableBinaryExpression(binary, this));
     }
     else if ((block = expression as BlockExpression) != null)
     {
         return(serialized[expression] = new SerializableBlockExpression(block, this));
     }
     else if ((conditional = expression as ConditionalExpression) != null)
     {
         return(serialized[expression] = new SerializableConditionalExpression(conditional, this));
     }
     else if ((@default = expression as DefaultExpression) != null)
     {
         return(serialized[expression] = new SerializableDefaultExpression(@default));
     }
     else if ((@goto = expression as GotoExpression) != null)
     {
         return(serialized[expression] = new SerializableGotoExpression(@goto, this));
     }
     else if ((index = expression as IndexExpression) != null)
     {
         return(serialized[expression] = new SerializableIndexExpression(index, this));
     }
     else if ((invocation = expression as InvocationExpression) != null)
     {
         return(serialized[expression] = new SerializableInvocationExpression(invocation, this));
     }
     else if ((label = expression as LabelExpression) != null)
     {
         return(serialized[expression] = new SerializableLabelExpression(label, this));
     }
     else if ((listInit = expression as ListInitExpression) != null)
     {
         return(serialized[expression] = new SerializableListInitExpression(listInit, this));
     }
     else if ((loop = expression as LoopExpression) != null)
     {
         return(serialized[expression] = new SerializableLoopExpression(loop, this));
     }
     else if ((memberInit = expression as MemberInitExpression) != null)
     {
         return(serialized[expression] = new SerializableMemberInitExpression(memberInit, this));
     }
     else if ((newArray = expression as NewArrayExpression) != null)
     {
         return(serialized[expression] = new SerializableNewArrayExpression(newArray, this));
     }
     else if ((@new = expression as NewExpression) != null)
     {
         return(serialized[expression] = new SerializableNewExpression(@new, this));
     }
     else if ((parameter = expression as ParameterExpression) != null)
     {
         return(serialized[expression] = new SerializableParameterExpression(parameter));
     }
     else if ((runtimeVariables = expression as RuntimeVariablesExpression) != null)
     {
         return(serialized[expression] = new SerializableRuntimeVariablesExpression(runtimeVariables, this));
     }
     else if ((@switch = expression as SwitchExpression) != null)
     {
         return(serialized[expression] = new SerializableSwitchExpression(@switch, this));
     }
     else if ((@try = expression as TryExpression) != null)
     {
         return(serialized[expression] = new SerializableTryExpression(@try, this));
     }
     else if ((typeBinary = expression as TypeBinaryExpression) != null)
     {
         return(serialized[expression] = new SerializableTypeBinaryExpression(typeBinary, this));
     }
     else if ((unary = expression as UnaryExpression) != null)
     {
         return(serialized[expression] = new SerializableUnaryExpression(unary, this));
     }
     else
     {
         throw new ArgumentOutOfRangeException("expression");
     }
 }
Example #2
0
 protected internal abstract void VisitUnary(SerializableUnaryExpression node);
        protected internal override void VisitUnary(SerializableUnaryExpression node)
        {
            switch (node.NodeType)
            {
            case ExpressionType.TypeAs:
                Out("(");
                break;

            case ExpressionType.Not:
                Out("Not(");
                break;

            case ExpressionType.Negate:
            case ExpressionType.NegateChecked:
                Out("-");
                break;

            case ExpressionType.UnaryPlus:
                Out("+");
                break;

            case ExpressionType.Quote:
                break;

            case ExpressionType.Throw:
                Out("throw(");
                break;

            case ExpressionType.Increment:
                Out("Increment(");
                break;

            case ExpressionType.Decrement:
                Out("Decrement(");
                break;

            case ExpressionType.PreIncrementAssign:
                Out("++");
                break;

            case ExpressionType.PreDecrementAssign:
                Out("--");
                break;

            case ExpressionType.OnesComplement:
                Out("~(");
                break;

            default:
                Out(node.NodeType.ToString());
                Out("(");
                break;
            }

            Visit(node.Operand);

            switch (node.NodeType)
            {
            case ExpressionType.Negate:
            case ExpressionType.NegateChecked:
            case ExpressionType.UnaryPlus:
            case ExpressionType.PreDecrementAssign:
            case ExpressionType.PreIncrementAssign:
            case ExpressionType.Quote:
                break;

            case ExpressionType.TypeAs:
                Out(" As ");
                Out(node.Type.Name);
                Out(")");
                break;

            case ExpressionType.PostIncrementAssign:
                Out("++");
                break;

            case ExpressionType.PostDecrementAssign:
                Out("--");
                break;

            default:
                Out(")");
                break;
            }
        }