Пример #1
0
        private Expression BuildIndex(IIndexExpressionNode indexer)
        {
            if (indexer.Object == null)
            {
                throw BindingExceptionManager.UnexpectedExpressionNode(indexer);
            }
            var target = BuildExpression(indexer.Object);

            if (target.Type.IsArray)
            {
                return(Expression.ArrayIndex(target, indexer.Arguments.Select(BuildExpression)));
            }

            var type       = GetTargetType(ref target);
            var targetData = new ArgumentData(indexer.Object, target, type, target == null);
            var args       = indexer
                             .Arguments
                             .ToArrayEx(node => new ArgumentData(node, node.NodeType == ExpressionNodeType.Lambda ? null : BuildExpression(node), null, false));

            var exp = TryGenerateMethodCall(targetData.FindIndexer(args, target == null), targetData, args);

            if (exp != null)
            {
                return(exp);
            }
            var arrayArgs = new Expression[args.Length];

            for (int i = 0; i < args.Length; i++)
            {
                var data = args[i];
                if (data.IsLambda || data.Expression == null)
                {
                    throw BindingExceptionManager.InvalidBindingMember(type, ReflectionExtensions.IndexerName);
                }
                arrayArgs[i] = ExpressionReflectionManager.ConvertIfNeed(data.Expression, typeof(object), false);
            }
            return(Expression.Call(GetIndexValueDynamicMethod,
                                   ExpressionReflectionManager.ConvertIfNeed(target, typeof(object), false),
                                   Expression.NewArrayInit(typeof(object), arrayArgs), Expression.Constant(new MethodInvoker()), DataContextParameter));
        }
Пример #2
0
        private Expression BuildIndex(IIndexExpressionNode indexer)
        {
            if (indexer.Object == null)
            {
                throw BindingExceptionManager.UnexpectedExpressionNode(indexer);
            }
            var target = BuildExpression(indexer.Object);

            if (target.Type.IsArray)
            {
                return(Expression.ArrayIndex(target, indexer.Arguments.Select(BuildExpression)));
            }

            var type       = GetTargetType(ref target);
            var targetData = new ArgumentData(indexer.Object, target, type);
            var args       = indexer
                             .Arguments
                             .ToArrayEx(node => new ArgumentData(node, node.NodeType == ExpressionNodeType.Lambda ? null : BuildExpression(node), null));

            var method = targetData.FindIndexer(args, target == null);

            return(GenerateMethodCall(method, targetData, args));
        }