コード例 #1
0
        private static bool TryGetBinding(object bindingNode, BindingContext bindingContext, out MemberBinding memberBinding, out Exception bindingError)
        {
            bindingError  = null;
            memberBinding = null;
            var bindingNodeTree = bindingNode as SyntaxTreeNode;

            if (bindingNodeTree == null)
            {
                return(false);
            }

            var bindingType = (string)bindingNodeTree.GetTypeName(throwOnError: true);
            var memberObj   = bindingNodeTree.GetMember(throwOnError: true);
            var member      = default(MemberDescription);

            if (bindingContext.TryResolveMember(memberObj, out member) == false)
            {
                return(false);
            }
            var memberValueType = TypeDescription.GetTypeDescription(member.ResultType);

            // ReSharper disable once SwitchStatementMissingSomeCases
            switch (bindingType)
            {
            case "Assignment":
                var expressionNode = bindingNodeTree.GetExpression(throwOnError: true);
                var expression     = default(Expression);
                if (AnyBinder.TryBindInNewScope(expressionNode, bindingContext, memberValueType, out expression, out bindingError) == false)
                {
                    return(false);                            // file to bind member's value
                }

                if (member.IsMethod)
                {
                    memberBinding = Expression.Bind((MethodInfo)member, expression);
                }
                else
                {
                    memberBinding = Expression.Bind((MemberInfo)member, expression);
                }
                return(true);

            case "MemberBinding":
                var bindings = default(MemberBinding[]);
                if (TryGetBindings(bindingNodeTree, bindingContext, out bindings, out bindingError) == false)
                {
                    return(false);                            // failed to resolve bindings
                }
                if (member.IsMethod)
                {
                    memberBinding = Expression.MemberBind((MethodInfo)member, bindings);
                }
                else
                {
                    memberBinding = Expression.MemberBind((MemberInfo)member, bindings);
                }
                return(true);

            case "ListBinding":
                var initializers = default(ElementInit[]);
                if (ListInitBinder.TryGetListInitializers(bindingNodeTree, bindingContext, out initializers, out bindingError) == false)
                {
                    return(false);                            // failed to resolve list initializers
                }
                if (member.IsMethod)
                {
                    memberBinding = Expression.ListBind((MethodInfo)member, initializers);
                }
                else
                {
                    memberBinding = Expression.ListBind((MemberInfo)member, initializers);
                }
                return(true);
            }

            return(false);
        }
コード例 #2
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }
            if (bindingContext == null)
            {
                throw new ArgumentNullException("bindingContext");
            }
            if (expectedType == null)
            {
                throw new ArgumentNullException("expectedType");
            }

            try
            {
                var expressionType = node.GetExpressionType(throwOnError: true);
                switch (expressionType)
                {
                case Constants.EXPRESSION_TYPE_MEMBER_RESOLVE:
                case Constants.EXPRESSION_TYPE_PROPERTY_OR_FIELD:
                    return(MemberBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CONSTANT:
                    return(ConstantBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CALL:
                    return(CallBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case "Enclose":
                case Constants.EXPRESSION_TYPE_UNCHECKED_SCOPE:
                case Constants.EXPRESSION_TYPE_CHECKED_SCOPE:
                case Constants.EXPRESSION_TYPE_GROUP:
                    return(GroupBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_INVOKE:
                    return(InvokeBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_LAMBDA:
                    return(LambdaBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_INDEX:
                    return(IndexBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_TYPE_OF:
                    return(TypeOfBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CONVERT:
                case Constants.EXPRESSION_TYPE_CONVERT_CHECKED:
                case Constants.EXPRESSION_TYPE_TYPE_IS:
                case Constants.EXPRESSION_TYPE_TYPE_AS:
                    return(TypeBinaryBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_DEFAULT:
                    return(DefaultBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_NEW:
                    return(NewBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_NEW_ARRAY_BOUNDS:
                    return(NewArrayBoundsBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_NEW_ARRAY_INIT:
                    return(NewArrayInitBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_MEMBER_INIT:
                    return(MemberInitBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_LIST_INIT:
                    return(ListInitBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_ADD:
                case Constants.EXPRESSION_TYPE_ADD_CHECKED:
                case Constants.EXPRESSION_TYPE_SUBTRACT:
                case Constants.EXPRESSION_TYPE_SUBTRACT_CHECKED:
                case Constants.EXPRESSION_TYPE_LEFT_SHIFT:
                case Constants.EXPRESSION_TYPE_RIGHT_SHIFT:
                case Constants.EXPRESSION_TYPE_GREATER_THAN:
                case Constants.EXPRESSION_TYPE_GREATER_THAN_OR_EQUAL:
                case Constants.EXPRESSION_TYPE_LESS_THAN:
                case Constants.EXPRESSION_TYPE_LESS_THAN_OR_EQUAL:
                case Constants.EXPRESSION_TYPE_POWER:
                case Constants.EXPRESSION_TYPE_DIVIDE:
                case Constants.EXPRESSION_TYPE_MULTIPLY:
                case Constants.EXPRESSION_TYPE_MULTIPLY_CHECKED:
                case Constants.EXPRESSION_TYPE_MODULO:
                case Constants.EXPRESSION_TYPE_EQUAL:
                case Constants.EXPRESSION_TYPE_NOT_EQUAL:
                case Constants.EXPRESSION_TYPE_AND:
                case Constants.EXPRESSION_TYPE_OR:
                case Constants.EXPRESSION_TYPE_EXCLUSIVE_OR:
                case Constants.EXPRESSION_TYPE_AND_ALSO:
                case Constants.EXPRESSION_TYPE_OR_ELSE:
                case Constants.EXPRESSION_TYPE_COALESCE:
                    return(BinaryBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_NEGATE:
                case Constants.EXPRESSION_TYPE_NEGATE_CHECKED:
                case Constants.EXPRESSION_TYPE_COMPLEMENT:
                case Constants.EXPRESSION_TYPE_NOT:
                case Constants.EXPRESSION_TYPE_UNARY_PLUS:
                case Constants.EXPRESSION_TYPE_ARRAY_LENGTH:
                    return(UnaryBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_CONDITION:
                    return(ConditionBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_PARAMETER:
                    return(ParameterBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                case Constants.EXPRESSION_TYPE_QUOTE:
                    return(QuoteBinder.TryBind(node, bindingContext, expectedType, out boundExpression, out bindingError));

                default:
                    boundExpression = null;
                    bindingError    = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNKNOWNEXPRTYPE, expressionType), node);
                    return(false);
                }
            }
            catch (ExpressionParserException error)
            {
                boundExpression = null;
                bindingError    = error;
                return(false);
            }
            catch (Exception error)
            {
                boundExpression = null;
                bindingError    = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_FAILEDTOBIND, node.GetExpressionType(throwOnError: false) ?? "<unknown>", error.Message), error, node);
                return(false);
            }
        }