public static string[] ExtractArgumentNames(SyntaxTreeNode node)
        {
            var arguments     = node.GetArguments(throwOnError: false);
            var argumentNames = new string[arguments.Count];

            for (var i = 0; i < argumentNames.Length; i++)
            {
                var argumentNameTree = default(SyntaxTreeNode);
                if (arguments.TryGetValue(i, out argumentNameTree) == false || argumentNameTree == null)
                {
                    throw new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_MISSINGATTRONNODE, Constants.EXPRESSION_ATTRIBUTE, Constants.EXPRESSION_TYPE_LAMBDA), node);
                }

                var argumentNameType = argumentNameTree.GetExpressionType(throwOnError: true);
                if (argumentNameType != Constants.EXPRESSION_TYPE_PROPERTY_OR_FIELD &&
                    argumentNameType != Constants.EXPRESSION_TYPE_MEMBER_RESOLVE &&
                    argumentNameType != Constants.EXPRESSION_TYPE_PARAMETER)
                {
                    throw new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_INVALIDLAMBDAPARAMETERTYPE, argumentNameType, Constants.EXPRESSION_TYPE_PARAMETER), node);
                }

                argumentNames[i] = argumentNameTree.GetMemberName(throwOnError: true);
            }
            return(argumentNames);
        }
        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");
            }


            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            var arguments  = node.GetArguments(throwOnError: false);
            var methodName = node.GetMethodName(throwOnError: false);

            if (methodName != null)
            {
                return(TryBindToMethod(node, methodName, arguments, bindingContext, expectedType, out boundExpression, out bindingError));
            }
            else
            {
                var typeName = node.GetTypeName(throwOnError: true);
                return(TryBindToType(node, typeName, arguments, bindingContext, expectedType, out boundExpression, out bindingError));
            }
        }
Exemple #3
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");
            }

            if (TryBindMethodCall(node, bindingContext, expectedType, out boundExpression, out bindingError))
            {
                return(true);
            }

            var targetNode = node.GetExpression(throwOnError: true);
            var arguments  = node.GetArguments(throwOnError: false);
            var target     = default(Expression);

            if (AnyBinder.TryBind(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(target != null, "target != null");

            var typeDescription = TypeDescription.GetTypeDescription(target.Type);

            if (typeDescription.IsDelegate == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOINVOKENONDELEG, target.Type), node);
                return(false);
            }

            var methodDescription = typeDescription.GetMembers(Constants.DELEGATE_INVOKE_NAME).FirstOrDefault(m => m.IsMethod && !m.IsStatic);

            if (methodDescription == null)
            {
                throw new MissingMethodException(string.Format(Properties.Resources.EXCEPTION_BIND_MISSINGMETHOD, target.Type.FullName, Constants.DELEGATE_INVOKE_NAME));
            }

            var expressionQuality = 0.0f;

            if (methodDescription.TryMakeCall(target, arguments, bindingContext, out boundExpression, out expressionQuality) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDDELEG, target.Type, methodDescription), node);
                return(false);
            }

            return(true);
        }
        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");
            }

            boundExpression = null;
            bindingError    = null;

            var typeName = node.GetTypeName(throwOnError: true);
            var type     = default(Type);

            if (bindingContext.TryResolveType(typeName, out type) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeName), node);
                return(false);
            }

            var indexTypeDescription = TypeDescription.Int32Type;
            var arguments            = node.GetArguments(throwOnError: true);
            var argumentExpressions  = new Expression[arguments.Count];

            for (var i = 0; i < arguments.Count; i++)
            {
                var argument = default(SyntaxTreeNode);
                if (arguments.TryGetValue(i, out argument) == false)
                {
                    bindingError = new ExpressionParserException(Properties.Resources.EXCEPTION_BOUNDEXPR_ARGSDOESNTMATCHPARAMS, node);
                    return(false);
                }

                if (AnyBinder.TryBindInNewScope(argument, bindingContext, indexTypeDescription, out argumentExpressions[i], out bindingError) == false)
                {
                    return(false);
                }

                Debug.Assert(argumentExpressions[i] != null, "argumentExpressions[i] != null");
            }

            boundExpression = Expression.NewArrayBounds(type, argumentExpressions);
            return(true);
        }
Exemple #5
0
        private static void RenderPropertyOrField(SyntaxTreeNode syntaxTree, StringBuilder builder, bool checkedScope)
        {
            if (syntaxTree == null)
            {
                throw new ArgumentNullException("syntaxTree");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            var target = syntaxTree.GetExpression(throwOnError: false);
            var propertyOrFieldName = syntaxTree.GetMemberName(throwOnError: true);
            var useNullPropagation  = syntaxTree.GetUseNullPropagation(throwOnError: false);
            var arguments           = syntaxTree.GetArguments(throwOnError: false);

            if (target != null)
            {
                Render(target, builder, false, checkedScope);
                if (useNullPropagation)
                {
                    builder.Append("?.");
                }
                else
                {
                    builder.Append(".");
                }
            }
            builder.Append(propertyOrFieldName);
            if (arguments != null && arguments.Count > 0)
            {
                builder.Append("<");
                for (var i = 0; i < arguments.Count; i++)
                {
                    if (i != 0)
                    {
                        builder.Append(",");
                    }
                    var typeArgument = default(SyntaxTreeNode);
                    if (arguments.TryGetValue(i, out typeArgument))
                    {
                        Render(typeArgument, builder, true, checkedScope);
                    }
                }
                builder.Append(">");
            }
        }
Exemple #6
0
        private static void RenderLambda(SyntaxTreeNode syntaxTree, StringBuilder builder, bool wrapped, bool checkedScope)
        {
            if (syntaxTree == null)
            {
                throw new ArgumentException("syntaxTree");
            }
            if (builder == null)
            {
                throw new ArgumentException("builder");
            }

            if (!wrapped)
            {
                builder.Append("(");
            }

            var arguments = syntaxTree.GetArguments(throwOnError: false);
            var body      = syntaxTree.GetExpression(throwOnError: true);

            if (arguments.Count != 1)
            {
                builder.Append("(");
            }
            var firstParam = true;

            foreach (var param in arguments.Values)
            {
                if (firstParam == false)
                {
                    builder.Append(", ");
                }
                Render(param, builder, true, checkedScope);
                firstParam = false;
            }
            if (arguments.Count != 1)
            {
                builder.Append(")");
            }
            builder.Append(" => ");
            Render(body, builder, false, checkedScope);

            if (!wrapped)
            {
                builder.Append(")");
            }
        }
Exemple #7
0
        private static void RenderInvokeOrIndex(SyntaxTreeNode syntaxTree, StringBuilder builder, bool checkedScope)
        {
            if (syntaxTree == null)
            {
                throw new ArgumentNullException("syntaxTree");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            var expressionType     = syntaxTree.GetExpressionType(throwOnError: true);
            var target             = syntaxTree.GetExpression(throwOnError: true);
            var arguments          = syntaxTree.GetArguments(throwOnError: false);
            var useNullPropagation = syntaxTree.GetUseNullPropagation(throwOnError: false);

            Render(target, builder, false, checkedScope);
            builder.Append(expressionType == Constants.DELEGATE_INVOKE_NAME ? "(" : (useNullPropagation ? "?[" : "["));
            RenderArguments(arguments, builder, checkedScope);
            builder.Append(expressionType == Constants.DELEGATE_INVOKE_NAME ? ")" : "]");
        }
Exemple #8
0
        private static void RenderNew(SyntaxTreeNode syntaxTree, StringBuilder builder, bool checkedScope)
        {
            if (syntaxTree == null)
            {
                throw new ArgumentNullException("syntaxTree");
            }
            if (builder == null)
            {
                throw new ArgumentNullException("builder");
            }

            var expressionType = syntaxTree.GetExpressionType(throwOnError: true);
            var typeName       = syntaxTree.GetTypeName(throwOnError: true);
            var arguments      = syntaxTree.GetArguments(throwOnError: false);

            builder.Append("new ");
            RenderTypeName(typeName, builder);
            if (expressionType == Constants.EXPRESSION_TYPE_NEW_ARRAY_BOUNDS)
            {
                builder.Append("[");
            }
            else
            {
                builder.Append("(");
            }

            RenderArguments(arguments, builder, checkedScope);

            if (expressionType == Constants.EXPRESSION_TYPE_NEW_ARRAY_BOUNDS)
            {
                builder.Append("]");
            }
            else
            {
                builder.Append(")");
            }
        }
Exemple #9
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");
            }

            boundExpression = null;
            bindingError    = null;

            var target             = default(Expression);
            var arguments          = node.GetArguments(throwOnError: false);
            var methodName         = node.GetMethodName(throwOnError: true);
            var useNullPropagation = node.GetUseNullPropagation(throwOnError: false);

            var methodMember = default(MemberDescription);

            if (bindingContext.TryResolveMember(methodName, out methodMember))
            {
                if (methodMember.IsMethod == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_CALLMEMBERISNOTMETHOD, methodMember.Name, methodMember.DeclaringType), node);
                    return(false);
                }

                var targetNode = node.GetExpression(throwOnError: true);

                if (AnyBinder.TryBind(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError) == false)
                {
                    return(false);
                }

                float methodQuality;
                if (methodMember.TryMakeCall(target, arguments, bindingContext, out boundExpression, out methodQuality) == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDMETHOD, methodMember.Name, target.Type, arguments.Count), node);
                    return(false);
                }

                return(true);
            }


            var methodRef = default(TypeReference);

            if (BindingContext.TryGetMethodReference(methodName, out methodRef) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVENAME, methodName), node);
                return(false);
            }

            var targetType = default(Type);

            if (TryBindTarget(node, bindingContext, out target, out targetType, out bindingError) == false)
            {
                return(false);
            }

            var isStatic = target == null;
            var selectedMethodQuality = MemberDescription.QUALITY_INCOMPATIBLE;
            var hasGenericParameters  = methodRef.IsGenericType;
            var genericArguments      = default(Type[]);

            if (hasGenericParameters)
            {
                genericArguments = new Type[methodRef.TypeArguments.Count];
                for (var i = 0; i < genericArguments.Length; i++)
                {
                    var typeArgument = methodRef.TypeArguments[i];
                    if (bindingContext.TryResolveType(typeArgument, out genericArguments[i]) == false)
                    {
                        bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeArgument), node);
                        return(false);
                    }
                }
            }

            var targetTypeDescription = TypeDescription.GetTypeDescription(targetType);
            var foundMethod           = default(MethodInfo);

            foreach (var memberDescription in targetTypeDescription.GetMembers(methodRef.Name))
            {
                if (memberDescription.IsMethod == false)
                {
                    continue;
                }

                var methodDescription = memberDescription;
                var method            = (MethodInfo)memberDescription;

                foundMethod = foundMethod ?? method;

                if (method.IsStatic != isStatic || method.IsGenericMethod != hasGenericParameters)
                {
                    continue;
                }

                if (hasGenericParameters && memberDescription.GenericArgumentsCount != methodRef.TypeArguments.Count)
                {
                    continue;
                }

                if (hasGenericParameters)
                {
                    try
                    {
                        methodDescription = methodDescription.MakeGenericMethod(genericArguments);
                        method            = methodDescription;
                    }
                    catch (ArgumentException exception)
                    {
                        bindingError = exception;
                        continue;                         /* An element of typeArguments does not satisfy the constraints specified for the corresponding type parameter of the current generic method definition. */
                    }
                }


                var methodQuality        = 0.0f;
                var methodCallExpression = default(Expression);
                if (methodDescription.TryMakeCall(target, arguments, bindingContext, out methodCallExpression, out methodQuality) == false)
                {
                    continue;
                }

                if (float.IsNaN(methodQuality) || methodQuality <= selectedMethodQuality)
                {
                    continue;
                }

                boundExpression       = methodCallExpression;
                selectedMethodQuality = methodQuality;

                if (Math.Abs(methodQuality - MemberDescription.QUALITY_EXACT_MATCH) < float.Epsilon)
                {
                    break;                     // best match
                }
            }

            if (bindingError != null)
            {
                return(false);
            }

            if (boundExpression == null)
            {
                if (foundMethod != null)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDMETHOD, methodRef.Name, targetType, arguments.Count), node);
                }
                else
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDCALL, methodRef.Name, targetType, arguments.Count), node);
                }
                return(false);
            }

            if (useNullPropagation && target == null)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOAPPLYNULLCONDITIONALOPERATORONTYPEREF, targetType));
                return(false);
            }

            if (useNullPropagation && targetTypeDescription.CanBeNull)
            {
                bindingContext.RegisterNullPropagationTarget(target);
            }

            if (targetTypeDescription.IsAssignableFrom(typeof(Type)) &&
                bindingContext.IsKnownType(typeof(Type)) == false &&
                (bindingContext.IsKnownType(targetType) == false || methodRef.Name.Equals("InvokeMember", StringComparison.Ordinal)))
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_RESTRICTED_MEMBER_INVOCATION, methodName, targetType, typeof(ITypeResolver)), node);
                return(false);
            }

            return(true);
        }
        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");
            }

            boundExpression = null;
            bindingError    = null;

            // try get type of lambda from node
            var lambdaTypeName = node.GetTypeName(throwOnError: false);
            var lambdaType     = default(Type);

            if (lambdaTypeName != null)
            {
                if (bindingContext.TryResolveType(lambdaTypeName, out lambdaType) == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, lambdaTypeName), node);
                    return(false);
                }
                else
                {
                    expectedType = TypeDescription.GetTypeDescription(lambdaType);
                }
            }

            if (expectedType.HasGenericParameters || !expectedType.IsDelegate)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_VALIDDELEGATETYPEISEXPECTED, expectedType.ToString()));
                return(false);
            }

            var expressionType     = node.GetExpressionType(throwOnError: true);
            var bodyNode           = node.GetExpression(throwOnError: true);
            var argumentsTree      = node.GetArguments(throwOnError: false);
            var lambdaInvokeMethod = expectedType.GetMembers(Constants.DELEGATE_INVOKE_NAME).FirstOrDefault(m => m.IsMethod && !m.IsStatic);

            if (lambdaInvokeMethod == null)
            {
                bindingError = new MissingMethodException(string.Format(Resources.EXCEPTION_BIND_MISSINGMETHOD, expectedType.ToString(), Constants.DELEGATE_INVOKE_NAME));
                return(false);
            }

            if (lambdaInvokeMethod.GetParametersCount() != argumentsTree.Count)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_INVALIDLAMBDAARGUMENTS, expectedType));
                return(false);
            }

            var argumentNames = new string[argumentsTree.Count];

            for (var i = 0; i < argumentNames.Length; i++)
            {
                var argumentNameTree     = default(SyntaxTreeNode);
                var argumentNameTreeType = default(string);
                if (argumentsTree.TryGetValue(i, out argumentNameTree) == false ||
                    argumentNameTree == null ||
                    (argumentNameTreeType = argumentNameTree.GetExpressionType(throwOnError: true)) == null ||
                    (argumentNameTreeType == Constants.EXPRESSION_TYPE_PROPERTY_OR_FIELD ||
                     argumentNameTreeType == Constants.EXPRESSION_TYPE_MEMBER_RESOLVE ||
                     argumentNameTreeType == Constants.EXPRESSION_TYPE_PARAMETER) == false)
                {
                    bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_MISSINGATTRONNODE, Constants.EXPRESSION_ATTRIBUTE, expressionType), node);
                    return(false);
                }
                argumentNames[i] = argumentNameTree.GetMemberName(throwOnError: true);
            }

            var lambdaParameters = new ParameterExpression[argumentsTree.Count];

            for (var i = 0; i < argumentsTree.Count; i++)
            {
                lambdaParameters[i] = Expression.Parameter(lambdaInvokeMethod.GetParameter(i).ParameterType, argumentNames[i]);
            }

            var currentParameters = bindingContext.Parameters;
            var newParameters     = new List <ParameterExpression>(lambdaParameters.Length + currentParameters.Count);

            // add all lambda's parameters
            newParameters.AddRange(lambdaParameters);
            // add closure parameters
            foreach (var parameterExpr in currentParameters)
            {
                if (Array.IndexOf(argumentNames, parameterExpr.Name) < 0)
                {
                    newParameters.Add(parameterExpr);
                }
            }

            var nestedBindingContext = bindingContext.CreateNestedContext(newParameters.AsReadOnly(), lambdaInvokeMethod.ResultType);
            var body = default(Expression);

            if (AnyBinder.TryBindInNewScope(bodyNode, nestedBindingContext, TypeDescription.GetTypeDescription(lambdaInvokeMethod.ResultType), out body, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(body != null, "body != null");

            boundExpression = Expression.Lambda(expectedType, body, lambdaParameters);
            return(true);
        }
Exemple #11
0
        public static bool TryBind(SyntaxTreeNode node, BindingContext bindingContext, TypeDescription expectedType, out Expression boundExpression, out Exception bindingError)
        {
            boundExpression = null;
            bindingError    = null;

            var useNullPropagation = node.GetUseNullPropagation(throwOnError: false);
            var arguments          = node.GetArguments(throwOnError: true);
            var targetNode         = node.GetExpression(throwOnError: true);
            var target             = default(Expression);

            if (AnyBinder.TryBind(targetNode, bindingContext, TypeDescription.ObjectType, out target, out bindingError) == false)
            {
                return(false);
            }

            Debug.Assert(target != null, "target != null");

            var targetTypeDescription = TypeDescription.GetTypeDescription(target.Type);

            if (target.Type.IsArray)
            {
                var indexType           = TypeDescription.Int32Type;
                var indexingExpressions = new Expression[arguments.Count];
                for (var i = 0; i < indexingExpressions.Length; i++)
                {
                    var argument = default(SyntaxTreeNode);
                    if (arguments.TryGetValue(i, out argument) == false)
                    {
                        bindingError = new ExpressionParserException(string.Format(Resources.EXCEPTION_BIND_MISSINGMETHODPARAMETER, i), node);
                        return(false);
                    }

                    if (AnyBinder.TryBindInNewScope(argument, bindingContext, indexType, out indexingExpressions[i], out bindingError) == false)
                    {
                        return(false);
                    }

                    Debug.Assert(indexingExpressions[i] != null, "indexingExpressions[i] != null");
                }

                try
                {
                    if (indexingExpressions.Length == 1)
                    {
                        boundExpression = Expression.ArrayIndex(target, indexingExpressions[0]);
                    }
                    else
                    {
                        boundExpression = Expression.ArrayIndex(target, indexingExpressions);
                    }
                }
                catch (Exception exception)
                {
                    bindingError = new ExpressionParserException(exception.Message, exception, node);
                    return(false);
                }
            }
            else
            {
                var selectedIndexerQuality = MemberDescription.QUALITY_INCOMPATIBLE;
                foreach (var indexer in targetTypeDescription.Indexers)
                {
                    var indexerQuality = MemberDescription.QUALITY_INCOMPATIBLE;
                    var indexerCall    = default(Expression);
                    if (indexer.TryMakeCall(target, arguments, bindingContext, out indexerCall, out indexerQuality) == false)
                    {
                        continue;
                    }
                    if (indexerQuality <= selectedIndexerQuality)
                    {
                        continue;
                    }

                    boundExpression        = indexerCall;
                    selectedIndexerQuality = indexerQuality;
                }
            }
            if (boundExpression == null)
            {
                bindingError = new ExpressionParserException(string.Format(Resources.EXCEPTION_BIND_UNABLETOBINDINDEXER, target.Type), node);
                return(false);
            }

            if (useNullPropagation && targetTypeDescription.CanBeNull)
            {
                bindingContext.RegisterNullPropagationTarget(target);
            }

            return(true);
        }
        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");
            }

            boundExpression = null;
            bindingError    = null;

            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            var arguments = node.GetArguments(throwOnError: false);
            var typeName  = node.GetTypeName(throwOnError: true);
            var type      = default(Type);

            if (bindingContext.TryResolveType(typeName, out type) == false)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETORESOLVETYPE, typeName), node);
                return(false);
            }

            var typeDescription = TypeDescription.GetTypeDescription(type);

            // feature: lambda building via new Func()
            var lambdaArgument = default(SyntaxTreeNode);

            if (typeDescription.IsDelegate && arguments.Count == 1 && (lambdaArgument = arguments.Values.Single()).GetExpressionType(throwOnError: true) == Constants.EXPRESSION_TYPE_LAMBDA)
            {
                return(LambdaBinder.TryBind(lambdaArgument, bindingContext, typeDescription, out boundExpression, out bindingError));
            }

            var selectedConstructorQuality = MemberDescription.QUALITY_INCOMPATIBLE;

            foreach (var constructorDescription in typeDescription.Constructors)
            {
                var constructorQuality = MemberDescription.QUALITY_INCOMPATIBLE;
                var constructorCall    = default(Expression);
                if (constructorDescription.TryMakeCall(null, arguments, bindingContext, out constructorCall, out constructorQuality) == false)
                {
                    continue;
                }

                if (float.IsNaN(constructorQuality) || constructorQuality <= selectedConstructorQuality)
                {
                    continue;
                }

                boundExpression            = constructorCall;
                selectedConstructorQuality = constructorQuality;

                if (Math.Abs(constructorQuality - MemberDescription.QUALITY_EXACT_MATCH) < float.Epsilon)
                {
                    break;                     // best match
                }
            }

            if (boundExpression == null)
            {
                bindingError = new ExpressionParserException(string.Format(Properties.Resources.EXCEPTION_BIND_UNABLETOBINDCONSTRUCTOR, type), node);
                return(false);
            }

            return(true);
        }