public ExtendedMemberInitExpression Update(ExtendedNewExpression newExpression, IEnumerable <Expression> arguments)
        {
            // TODO: Argument validation

            if (newExpression != NewExpression || arguments != Arguments)
            {
                return(new ExtendedMemberInitExpression(Type, newExpression, arguments, ReadableMembers, WritableMembers));
            }

            return(this);
        }
        public ExtendedMemberInitExpression(
            Type explicitType,
            ExtendedNewExpression newExpression,
            IEnumerable <Expression> arguments,
            IEnumerable <MemberInfo> readableMembers,
            IEnumerable <MemberInfo> writableMembers)
            : this(newExpression, arguments, readableMembers, writableMembers)
        {
            Type = explicitType;

            if (!Type.IsAssignableFrom(newExpression.Type))
            {
                throw new ArgumentException();
            }
        }
Пример #3
0
        public ExtendedMemberInitExpression Update(ExtendedNewExpression newExpression, IEnumerable <Expression> arguments)
        {
            if (newExpression == null)
            {
                throw new ArgumentNullException(nameof(newExpression));
            }

            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }

            if (newExpression != NewExpression || arguments != Arguments)
            {
                return(new ExtendedMemberInitExpression(Type, newExpression, arguments, ReadableMembers, WritableMembers));
            }

            return(this);
        }
        public ExtendedMemberInitExpression(
            ExtendedNewExpression newExpression,
            IEnumerable <Expression> arguments,
            IEnumerable <MemberInfo> readableMembers,
            IEnumerable <MemberInfo> writableMembers)
        {
            // TODO: Argument validation

            arguments       = arguments?.ToArray();
            readableMembers = readableMembers?.ToArray();
            writableMembers = writableMembers?.ToArray();

            Type            = newExpression.Type;
            NewExpression   = newExpression ?? throw new ArgumentNullException(nameof(newExpression));
            Arguments       = new ReadOnlyCollection <Expression>(arguments?.ToArray() ?? throw new ArgumentNullException(nameof(arguments)));
            ReadableMembers = new ReadOnlyCollection <MemberInfo>(readableMembers?.ToArray() ?? throw new ArgumentNullException(nameof(readableMembers)));
            WritableMembers = new ReadOnlyCollection <MemberInfo>(writableMembers?.ToArray() ?? throw new ArgumentNullException(nameof(writableMembers)));

            if (ReadableMembers.Count != Arguments.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(readableMembers));
            }

            if (WritableMembers.Count != Arguments.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(writableMembers));
            }

            for (var i = 0; i < Arguments.Count; i++)
            {
                var argument = Arguments[i];

                if (Arguments[i] is null)
                {
                    throw new ArgumentException($"Arguments cannot be null. Element {i} is null.");
                }

                if (ReadableMembers[i] is null)
                {
                    throw new ArgumentException($"ReadableMembers cannot be null. Element {i} is null.");
                }

                if (WritableMembers[i] is null)
                {
                    throw new ArgumentException($"WritableMembers cannot be null. Element {i} is null.");
                }

                if (!(ReadableMembers[i].GetMemberType().IsAssignableFrom(Arguments[i].Type)))
                {
                    throw new ArgumentException($"Arguments and ReadableMembers must match. Elements at index {i} do not match.");
                }

                if (!(WritableMembers[i].GetMemberType().IsAssignableFrom(Arguments[i].Type)))
                {
                    throw new ArgumentException($"Arguments and WritableMembers must match. Elements at index {i} do not match.");
                }

                if (!(ReadableMembers[i].DeclaringType.IsAssignableFrom(newExpression.Type)))
                {
                    throw new ArgumentException($"ReadableMembers must be valid for the NewExpression. Element {i} is not valid.");
                }

                if (!(WritableMembers[i].DeclaringType.IsAssignableFrom(newExpression.Type)))
                {
                    throw new ArgumentException($"WritableMembers must be valid for the NewExpression. Element {i} is not valid.");
                }
            }
        }