private static Expression ReadFunction(XmlExpressionReader reader)
        {
            ParameterExpression[] variables = null;
            Expression            body      = null;

            string name       = reader.ReadAttributeValue("name", null);
            Type   returnType = reader.ReadAttributeValue <Type>("returnType", typeof(void));

            if (!string.IsNullOrEmpty(name))
            {
                reader.Context.AddVariable(returnType, name);
            }

            var exprs = reader.ReadChildExpressions(out variables);

            if (exprs == null || exprs.Length == 0)
            {
                throw new NotBodyNodeException(reader.Node);
            }
            if (exprs != null && exprs.Length > 0)
            {
                body = exprs[0];
            }

            return(Function(name, variables, body, returnType));
        }
        private static ParameterExpression ReadDefineVariable(XmlExpressionReader reader)
        {
            Type   type = reader.ReadAttributeValue <Type>("type");
            string name = reader.ReadInnerText();

            reader.Context.AddVariable(type, name);

            return(Variable(type, name));
        }
        private static Expression ReadConstant(XmlExpressionReader reader)
        {
            Type   type = reader.ReadAttributeValue <Type>("type");
            object value;
            var    node = reader.Node;

            value = reader.ChangeType(node, reader.ReadInnerText(), type);
            return(Constant(value, type));
        }
        private static Expression ReadProperty(XmlExpressionReader reader)
        {
            string     propertyName = reader.ReadAttributeValue("name");
            Type       type         = reader.ReadAttributeValue <Type>("type", null);
            Expression instance;
            var        exprs = reader.ReadChildExpressions();

            if (exprs == null || exprs.Length == 0)
            {
                throw new MissingNodeException(Resource1.Read_NotInstanceOrTypeNode, reader.Node, null);
            }
            instance = exprs[0];
            //if (exprs[0] is ConstantExpression)
            //    type = ((ConstantExpression)exprs[0]).Value as Type;
            //if (type == null)
            //{
            //    return Property(exprs[0], propertyName);
            //}
            return(Property(instance, type ?? instance.ValueType, propertyName));
        }
        private static Expression ReadMethodCall(XmlExpressionReader reader)
        {
            string     methodName = reader.ReadAttributeValue("method", null);
            Expression callExpr;

            reader.PushScope();

            var exprs = reader.ReadChildExpressions();

            if (exprs == null || exprs.Length == 0)
            {
                throw new MissingNodeException(reader.Node, null);
            }

            if (string.IsNullOrEmpty(methodName))
            {
                Type returnType = exprs[0].ValueType;
                //function
                callExpr = Call(exprs[0], returnType, exprs.Skip(1));
            }
            else
            {
                Type       type     = reader.ReadAttributeValue <Type>("type", null);
                Type[]     argTypes = ReadTypes(reader.ReadAttributeValue("argTypes", null));
                Expression instance = exprs[0];
                //if (instance == Null)
                //    instance = null;
                //if (instance != null)
                //{
                callExpr = Call(instance, type ?? instance.ValueType, methodName, argTypes, exprs.Skip(1));
                //}
                //else
                //{
                //    callExpr = Call(type, methodName, argTypes, exprs.Skip(1));
                //}
            }

            reader.PopScope();
            return(callExpr);
        }