Ejemplo n.º 1
0
        protected internal ArrayInitializer(NRefactory.ArrayInitializerExpression arrayInitializerExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor)
        {
            TypeInformation typeInformation;
            MethodReference methodReference;

            _arrayInitializerExpression = arrayInitializerExpression;

            if (_arrayInitializerExpression.Parent.HasAnnotationOf <MethodReference>(out methodReference))
            {
                _constructor = methodReference.GetActualMethod() as ConstructorInfo;
                InternalType = _constructor.DeclaringType;

                if (_constructor.GetParameters().Length > 0)
                {
                    var newExpression = _arrayInitializerExpression.Parent as NRefactory.ObjectCreateExpression;

                    if (newExpression != null)
                    {
                        _arguments = newExpression.Arguments
                                     .Select(arg => arg.AcceptVisitor(Visitor, ParentScope).Reduce())
                                     .ToList();
                    }
                }
            }
            else if (_arrayInitializerExpression.Parent.HasAnnotationOf <TypeInformation>(out typeInformation))
            {
                ConstructorInfo[] ctors = null;

                InternalType = typeInformation.InferredType.GetActualType();
                ctors        = InternalType.GetConstructors();

                if (ctors.Length > 1)
                {
                    _constructor = ctors[0];
                }
            }

            if (InternalType != null && !InternalType.IsGenericListOrDictionary() && !InternalType.IsArray)
            {
                isPropertyAssignment = true;
            }

            _initializers = arrayInitializerExpression.Elements
                            .Select(e => e.AcceptVisitor(Visitor, ParentScope))
                            .ToList();
        }
        public override Expression Reduce()
        {
            var ctor = InternalType.GetConstructors()[0];

            return(Expression.New(ctor, _initializers));
        }