Esempio n. 1
0
        /// <summary>
        /// From the serializable.
        /// </summary>
        /// <param name="newExpressionRepresentation">The new expression.</param>
        /// <returns>
        /// Converted expression.
        /// </returns>
        public static NewExpression FromRepresentation(this NewExpressionRepresentation newExpressionRepresentation)
        {
            if (newExpressionRepresentation == null)
            {
                throw new ArgumentNullException(nameof(newExpressionRepresentation));
            }

            var type = newExpressionRepresentation.Type.ResolveFromLoadedTypes();

            NewExpression result;

            if (newExpressionRepresentation.ConstructorInfo != null)
            {
                var constructor = type.GetConstructors().Single(_ => _.ToRepresentation().Equals(newExpressionRepresentation.ConstructorInfo));

                var arguments = newExpressionRepresentation.Arguments.FromRepresentation();

                result = Expression.New(constructor, arguments);
            }
            else
            {
                result = Expression.New(type);
            }

            return(result);
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ListInitExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type of expression.</param>
        /// <param name="newExpressionRepresentation">The new expression.</param>
        /// <param name="initializers">The initializers.</param>
        public ListInitExpressionRepresentation(
            TypeRepresentation type,
            NewExpressionRepresentation newExpressionRepresentation,
            IReadOnlyList <ElementInitRepresentation> initializers)
            : base(type, ExpressionType.ListInit)
        {
            if (newExpressionRepresentation == null)
            {
                throw new ArgumentNullException(nameof(newExpressionRepresentation));
            }

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

            if (!initializers.Any())
            {
                throw new ArgumentException(Invariant($"'{nameof(initializers)}' is an empty enumerable"));
            }

            if (initializers.Any(_ => _ == null))
            {
                throw new ArgumentException(Invariant($"'{nameof(initializers)}' contains at least one null element"));
            }

            this.NewExpressionRepresentation = newExpressionRepresentation;
            this.Initializers = initializers;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MemberInitExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="newExpressionRepresentation">The new expression.</param>
        /// <param name="bindings">The bindings.</param>
        public MemberInitExpressionRepresentation(
            TypeRepresentation type,
            NewExpressionRepresentation newExpressionRepresentation,
            IReadOnlyCollection <MemberBindingRepresentationBase> bindings)
            : base(type, ExpressionType.MemberInit)
        {
            if (newExpressionRepresentation == null)
            {
                throw new ArgumentNullException(nameof(newExpressionRepresentation));
            }

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

            if (!bindings.Any())
            {
                throw new ArgumentException(Invariant($"'{nameof(bindings)}' is an empty enumerable"));
            }

            if (bindings.Any(_ => _ == null))
            {
                throw new ArgumentException(Invariant($"'{nameof(bindings)}' contains at least one null element"));
            }

            this.NewExpressionRepresentation = newExpressionRepresentation;
            this.Bindings = bindings;
        }
Esempio n. 4
0
        /// <summary>
        /// Converts to serializable.
        /// </summary>
        /// <param name="newExpression">The new expression.</param>
        /// <returns>
        /// Serializable expression.
        /// </returns>
        public static NewExpressionRepresentation ToRepresentation(
            this NewExpression newExpression)
        {
            if (newExpression == null)
            {
                throw new ArgumentNullException(nameof(newExpression));
            }

            var type = newExpression.Type.ToRepresentation();

            var constructorInfoRepresentation = newExpression.Constructor.ToRepresentation();

            var arguments = newExpression.Arguments.ToRepresentation();

            var result = new NewExpressionRepresentation(type, constructorInfoRepresentation, arguments);

            return(result);
        }
Esempio n. 5
0
        public MemberInitExpressionRepresentation DeepCloneWithNewExpressionRepresentation(NewExpressionRepresentation newExpressionRepresentation)
        {
            var result = new MemberInitExpressionRepresentation(
                this.Type?.DeepClone(),
                newExpressionRepresentation,
                this.Bindings?.DeepClone());

            return(result);
        }
Esempio n. 6
0
        public ListInitExpressionRepresentation DeepCloneWithNewExpressionRepresentation(NewExpressionRepresentation newExpressionRepresentation)
        {
            var result = new ListInitExpressionRepresentation(
                this.Type?.DeepClone(),
                newExpressionRepresentation,
                this.Initializers?.DeepClone());

            return(result);
        }