Exemplo n.º 1
0
 public Expression VisitArrayLiteral(ArrayListExpression node)
 {
     for (int i = 0; i < node.Expressions.Count; i++)
     {
         node.Expressions[i].Accept(this);
     }
     return(node);
 }
Exemplo n.º 2
0
        /// <inheritdoc/>
        Expression IExpressionVisitor <Expression> .VisitArrayLiteral(ArrayListExpression node)
        {
            var type = node.ArrayType != null?node.ArrayType.ResolveType(Context) : TypeProvider.AnyType;

            node.Type        = typeof(Collections.List <>).MakeGenericType(type);
            node.ElementType = type;
            if (node.Arguments != null)
            {
                var types = node.Arguments.Map(arg => arg.Accept(this).Type);
                if (node.Constructor == null)
                {
                    var ctors = node.Type.GetConstructors(ReflectionUtils.PublicInstance);
                    var ctor  = ReflectionUtils.BindToMethod(ctors, types, out ArgumentConversions conversions);
                    if (ctor == null)
                    {
                        ExecutionException.ThrowMissingMethod(node.Type, "ctor", node);
                    }
                    node.Constructor         = ctor;
                    node.ArgumentConversions = conversions;
                }
            }
            else if (node.Constructor == null)
            {
                node.Constructor = node.Type.GetConstructor(ReflectionUtils.PublicInstance, null, new Type[0], null);
            }
            var items = node.Expressions;

            if (items.Count > 0)
            {
                var arrayConversions = new ArgumentConversions(items.Count);
                for (int index = 0; index < items.Count; index++)
                {
                    var expression = items[index].Accept(this);
                    if (!TypeUtils.AreReferenceAssignable(type, expression.Type) && expression.Type.TryImplicitConvert(type, out System.Reflection.MethodInfo implicitCall))
                    {
                        if (expression.Type.IsValueType &&
                            implicitCall.GetParameters()[0].ParameterType == TypeProvider.ObjectType)
                        {
                            arrayConversions.Append(index, new BoxConversion(index, expression.Type));
                        }
                        arrayConversions.Append(index, new ParamConversion(index, implicitCall));
                    }
                }
                node.ArrayConversions = arrayConversions;
            }
            return(node);
        }
Exemplo n.º 3
0
        public virtual object VisitArrayLiteral(ArrayListExpression node)
        {
            Type type = node.ArrayType != null?node.ArrayType.ResolveType(TypeContext) : TypeProvider.AnyType;

            node.Type        = typeof(Collections.List <>).MakeGenericType(type);
            node.ElementType = type;
            var items  = node.Expressions;
            var length = items.Count;

            object[] args;
            if (node.Arguments != null)
            {
                args = node.Arguments.Map(arg => arg.Accept(this));
                if (node.Constructor is null)
                {
                    var methods = node.Type.GetConstructors(ReflectionUtils.PublicInstance);
                    var ctor    = ReflectionUtils.BindToMethod(methods, args, out ArgumentConversions conversions);
                    if (ctor is null)
                    {
                        ExecutionException.ThrowMissingMethod(node.Type, ".ctor", node);
                    }
                    node.Constructor         = ctor;
                    node.ArgumentConversions = conversions;
                }
                node.ArgumentConversions.Invoke(ref args);
            }
            else
            {
                args = new object[1] {
                    new Integer(length)
                };
                if (node.Constructor is null)
                {
                    node.Constructor = node.Type.GetConstructor(ReflectionUtils.PublicInstance, null, new Type[] { TypeProvider.IntType }, null);
                }
            }

            var array = (System.Collections.IList)node.Constructor.Invoke(System.Reflection.BindingFlags.Default, null, args, null);

            if (length > 0)
            {
                var arrayConversions = node.ArrayConversions ?? new ArgumentConversions(items.Count);
                for (int index = 0; index < length; index++)
                {
                    Expression expression = items[index];
                    var        value      = expression.Accept(this);
                    var        conversion = arrayConversions[index];
                    if (conversion == null && !TypeUtils.AreReferenceAssignable(type, expression.Type) && expression.Type.TryImplicitConvert(type, out System.Reflection.MethodInfo implicitCall))
                    {
                        conversion = new ParamConversion(index, implicitCall);
                        arrayConversions.Append(index, conversion);
                    }
                    if (conversion != null && conversion.ConversionType == ConversionType.Normal)
                    {
                        value = conversion.Invoke(value);
                    }
                    array.Add(value);
                }
                node.ArrayConversions = arrayConversions;
            }
            return(array);
        }