예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LambdaExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type of expression.</param>
        /// <param name="body">The body.</param>
        /// <param name="parameters">The parameters.</param>
        public LambdaExpressionRepresentation(
            TypeRepresentation type,
            ExpressionRepresentationBase body,
            IReadOnlyList <ParameterExpressionRepresentation> parameters)
            : base(type, ExpressionType.Lambda)
        {
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

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

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

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

            this.Body       = body;
            this.Parameters = parameters;
        }
예제 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InvocationExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type of expression.</param>
        /// <param name="expressionRepresentation">The expression to invoke.</param>
        /// <param name="arguments">The arguments to invoke with.</param>
        public InvocationExpressionRepresentation(
            TypeRepresentation type,
            ExpressionRepresentationBase expressionRepresentation,
            IReadOnlyList <ExpressionRepresentationBase> arguments)
            : base(type, ExpressionType.Invoke)
        {
            if (expressionRepresentation == null)
            {
                throw new ArgumentNullException(nameof(expressionRepresentation));
            }

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

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

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

            this.ExpressionRepresentation = expressionRepresentation;
            this.Arguments = arguments;
        }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConditionalExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type of expression.</param>
        /// <param name="nodeType">Type of the node.</param>
        /// <param name="test">The test expression.</param>
        /// <param name="ifTrue">If true expression.</param>
        /// <param name="ifFalse">If false expression.</param>
        #pragma warning disable SA1305 // Field names should not use Hungarian notation
        public ConditionalExpressionRepresentation(
            TypeRepresentation type,
            ExpressionType nodeType,
            ExpressionRepresentationBase test,
            ExpressionRepresentationBase ifTrue,
            ExpressionRepresentationBase ifFalse)
            : base(type, nodeType)
        {
            if (test == null)
            {
                throw new ArgumentNullException(nameof(test));
            }

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

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

            this.Test    = test;
            this.IfTrue  = ifTrue;
            this.IfFalse = ifFalse;
        }
        public TypeBinaryExpressionRepresentation DeepCloneWithExpression(ExpressionRepresentationBase expression)
        {
            var result = new TypeBinaryExpressionRepresentation(
                this.Type?.DeepClone(),
                expression);

            return(result);
        }
        public LambdaExpressionRepresentation DeepCloneWithBody(ExpressionRepresentationBase body)
        {
            var result = new LambdaExpressionRepresentation(
                this.Type?.DeepClone(),
                body,
                this.Parameters?.DeepClone());

            return(result);
        }
예제 #6
0
        public UnaryExpressionRepresentation DeepCloneWithOperand(ExpressionRepresentationBase operand)
        {
            var result = new UnaryExpressionRepresentation(
                this.Type?.DeepClone(),
                this.NodeType.DeepClone(),
                operand);

            return(result);
        }
예제 #7
0
        public MemberAssignmentRepresentation DeepCloneWithExpressionRepresentation(ExpressionRepresentationBase expressionRepresentation)
        {
            var result = new MemberAssignmentRepresentation(
                this.Type?.DeepClone(),
                this.MemberInfo?.DeepClone(),
                expressionRepresentation);

            return(result);
        }
        public BinaryExpressionRepresentation DeepCloneWithRight(ExpressionRepresentationBase right)
        {
            var result = new BinaryExpressionRepresentation(
                this.Type?.DeepClone(),
                this.NodeType.DeepClone(),
                this.Left?.DeepClone(),
                right);

            return(result);
        }
        public BinaryExpressionRepresentation DeepCloneWithLeft(ExpressionRepresentationBase left)
        {
            var result = new BinaryExpressionRepresentation(
                this.Type?.DeepClone(),
                this.NodeType.DeepClone(),
                left,
                this.Right?.DeepClone());

            return(result);
        }
예제 #10
0
        public ConditionalExpressionRepresentation DeepCloneWithTest(ExpressionRepresentationBase test)
        {
            var result = new ConditionalExpressionRepresentation(
                this.Type?.DeepClone(),
                this.NodeType.DeepClone(),
                test,
                this.IfTrue?.DeepClone(),
                this.IfFalse?.DeepClone());

            return(result);
        }
예제 #11
0
        public ConditionalExpressionRepresentation DeepCloneWithIfFalse(ExpressionRepresentationBase ifFalse)
        {
            var result = new ConditionalExpressionRepresentation(
                this.Type?.DeepClone(),
                this.NodeType.DeepClone(),
                this.Test?.DeepClone(),
                this.IfTrue?.DeepClone(),
                ifFalse);

            return(result);
        }
예제 #12
0
        public MethodCallExpressionRepresentation DeepCloneWithParentObject(ExpressionRepresentationBase parentObject)
        {
            var result = new MethodCallExpressionRepresentation(
                this.Type?.DeepClone(),
                this.NodeType.DeepClone(),
                parentObject,
                this.Method?.DeepClone(),
                this.Arguments?.DeepClone());

            return(result);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="TypeBinaryExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="expression">The expression.</param>
        public TypeBinaryExpressionRepresentation(
            TypeRepresentation type,
            ExpressionRepresentationBase expression)
            : base(type, ExpressionType.TypeIs)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            this.Expression = expression;
        }
예제 #14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemberAssignmentRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="memberInfo">The member hash.</param>
        /// <param name="expressionRepresentation">The expression.</param>
        public MemberAssignmentRepresentation(
            TypeRepresentation type,
            MemberInfoRepresentation memberInfo,
            ExpressionRepresentationBase expressionRepresentation)
            : base(type, memberInfo, MemberBindingType.Assignment)
        {
            if (expressionRepresentation == null)
            {
                throw new ArgumentNullException(nameof(expressionRepresentation));
            }

            this.ExpressionRepresentation = expressionRepresentation;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="UnaryExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="nodeType">Type of the node.</param>
        /// <param name="operand">The operand.</param>
        public UnaryExpressionRepresentation(
            TypeRepresentation type,
            ExpressionType nodeType,
            ExpressionRepresentationBase operand)
            : base(type, nodeType)
        {
            if (operand == null)
            {
                throw new ArgumentNullException(nameof(operand));
            }

            this.Operand = operand;
        }
        /// <summary>
        /// Visits all nodes in the tree.
        /// </summary>
        /// <param name="root">Node traverse from.</param>
        /// <returns>
        /// Collection of the <see cref="ExpressionRepresentationBase" /> from the tree.
        /// </returns>
        public static IReadOnlyCollection <ExpressionRepresentationBase> VisitAllNodes(
            this ExpressionRepresentationBase root)
        {
            var result = new List <ExpressionRepresentationBase>();

            foreach (var linkNode in root.VisitAllConnectedNodes())
            {
                result.AddRange(linkNode.VisitAllNodes());
            }

            result.Add(root);

            return(result);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="MemberExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="memberInfo">The member info representation.</param>
        public MemberExpressionRepresentation(
            TypeRepresentation type,
            ExpressionRepresentationBase expression,
            MemberInfoRepresentation memberInfo)
            : base(type, ExpressionType.MemberAccess)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

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

            this.Expression = expression;
            this.MemberInfo = memberInfo;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BinaryExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type of expression.</param>
        /// <param name="nodeType">Type of the node.</param>
        /// <param name="left">The left expression.</param>
        /// <param name="right">The right expression.</param>
        public BinaryExpressionRepresentation(
            TypeRepresentation type,
            ExpressionType nodeType,
            ExpressionRepresentationBase left,
            ExpressionRepresentationBase right)
            : base(type, nodeType)
        {
            if (left == null)
            {
                throw new ArgumentNullException(nameof(left));
            }

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

            this.Left  = left;
            this.Right = right;
        }
예제 #19
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MethodCallExpressionRepresentation"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="nodeType">Type of the node.</param>
        /// <param name="parentObject">The object.</param>
        /// <param name="method">The method.</param>
        /// <param name="arguments">The arguments.</param>
        public MethodCallExpressionRepresentation(
            TypeRepresentation type,
            ExpressionType nodeType,
            ExpressionRepresentationBase parentObject,
            MethodInfoRepresentation method,
            IReadOnlyList <ExpressionRepresentationBase> arguments)
            : base(type, nodeType)
        {
            if (parentObject == null)
            {
                throw new ArgumentNullException(nameof(parentObject));
            }

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

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

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

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

            this.ParentObject = parentObject;
            this.Method       = method;
            this.Arguments    = arguments;
        }
예제 #20
0
        public static Expression FromRepresentation(
            this ExpressionRepresentationBase expressionRepresentation)
        {
            if (expressionRepresentation == null)
            {
                throw new ArgumentNullException(nameof(expressionRepresentation));
            }

            if (expressionRepresentation is BinaryExpressionRepresentation binaryExpression)
            {
                return(binaryExpression.FromRepresentation());
            }
            else if (expressionRepresentation is ConditionalExpressionRepresentation conditionalExpression)
            {
                return(conditionalExpression.FromRepresentation());
            }
            else if (expressionRepresentation.GetType().IsGenericType&& expressionRepresentation.GetType().GetGenericTypeDefinition() == typeof(ConstantExpressionRepresentation <>))
            {
                var type = expressionRepresentation.Type.ResolveFromLoadedTypes();
                var conversionMethodGeneric =
                    typeof(ConstantExpressionRepresentationExtensions).GetMethod(
                        nameof(ConstantExpressionRepresentationExtensions.FromRepresentation)) ??
                    throw new ArgumentException(Invariant(
                                                    $"Method '{nameof(ConstantExpressionRepresentationExtensions)}.{nameof(ConstantExpressionRepresentationExtensions.FromRepresentation)}' should be there."));

                var conversionMethodReal = conversionMethodGeneric.MakeGenericMethod(type);
                var resultRaw            = conversionMethodReal.Invoke(null, new[] { expressionRepresentation });
                return((ConstantExpression)resultRaw);
            }
            else if (expressionRepresentation is InvocationExpressionRepresentation invocationExpression)
            {
                return(invocationExpression.FromRepresentation());
            }
            else if (expressionRepresentation is LambdaExpressionRepresentation lambdaExpression)
            {
                return(lambdaExpression.FromRepresentation());
            }
            else if (expressionRepresentation is ListInitExpressionRepresentation listInitExpression)
            {
                return(listInitExpression.FromRepresentation());
            }
            else if (expressionRepresentation is MemberExpressionRepresentation memberExpression)
            {
                return(memberExpression.FromRepresentation());
            }
            else if (expressionRepresentation is MemberInitExpressionRepresentation memberInitExpression)
            {
                return(memberInitExpression.FromRepresentation());
            }
            else if (expressionRepresentation is MethodCallExpressionRepresentation methodCallExpression)
            {
                return(methodCallExpression.FromRepresentation());
            }
            else if (expressionRepresentation is NewArrayExpressionRepresentation newArrayExpression)
            {
                return(newArrayExpression.FromRepresentation());
            }
            else if (expressionRepresentation is NewExpressionRepresentation newExpression)
            {
                return(newExpression.FromRepresentation());
            }
            else if (expressionRepresentation is ParameterExpressionRepresentation parameterExpression)
            {
                return(parameterExpression.FromRepresentation());
            }
            else if (expressionRepresentation is TypeBinaryExpressionRepresentation typeBinaryExpression)
            {
                return(typeBinaryExpression.FromRepresentation());
            }
            else if (expressionRepresentation is UnaryExpressionRepresentation unaryExpression)
            {
                return(unaryExpression.FromRepresentation());
            }
            else
            {
                throw new NotSupportedException(Invariant($"Expression type '{expressionRepresentation.GetType()}' is not supported."));
            }
        }
        /// <summary>
        /// Visits all connected nodes.
        /// </summary>
        /// <param name="root">The root.</param>
        /// <returns>
        /// Collection of the connected nodes.
        /// </returns>
        public static IReadOnlyCollection <ExpressionRepresentationBase> VisitAllConnectedNodes(
            this ExpressionRepresentationBase root)
        {
            var result = new List <ExpressionRepresentationBase>();

            switch (root)
            {
            case LambdaExpressionRepresentation lambdaExpressionRepresentation:
                result.Add(lambdaExpressionRepresentation.Body);
                result.AddRange(lambdaExpressionRepresentation.Parameters);

                break;

            case BinaryExpressionRepresentation binaryExpressionRepresentation:
                result.Add(binaryExpressionRepresentation.Left);
                result.Add(binaryExpressionRepresentation.Right);
                break;

            case ConditionalExpressionRepresentation conditionalExpressionRepresentation:
                result.Add(conditionalExpressionRepresentation.IfTrue);
                result.Add(conditionalExpressionRepresentation.IfFalse);
                result.Add(conditionalExpressionRepresentation.Test);
                break;

            case InvocationExpressionRepresentation invocationExpressionRepresentation:
            {
                result.AddRange(invocationExpressionRepresentation.Arguments);

                break;
            }

            case ListInitExpressionRepresentation listInitExpressionRepresentation:
                result.Add(listInitExpressionRepresentation.NewExpressionRepresentation);
                foreach (var x in listInitExpressionRepresentation.Initializers)
                {
                    result.AddRange(x.Arguments);
                }

                break;

            case MemberExpressionRepresentation memberExpressionRepresentation:
                result.Add(memberExpressionRepresentation.Expression);
                break;

            case MemberInitExpressionRepresentation memberInitExpressionRepresentation:
                result.Add(memberInitExpressionRepresentation.NewExpressionRepresentation);
                break;

            case MethodCallExpressionRepresentation methodCallExpressionRepresentation:
                result.AddRange(methodCallExpressionRepresentation.Arguments);
                result.Add(methodCallExpressionRepresentation.ParentObject);
                break;

            case NewArrayExpressionRepresentation newArrayExpressionRepresentation:
                result.AddRange(newArrayExpressionRepresentation.Expressions);
                break;

            case NewExpressionRepresentation newExpressionRepresentation:
            {
                result.AddRange(newExpressionRepresentation.Arguments);

                break;
            }

            case TypeBinaryExpressionRepresentation typeBinaryExpressionRepresentation:
                result.Add(typeBinaryExpressionRepresentation.Expression);
                break;

            case UnaryExpressionRepresentation unaryExpressionRepresentation:
                result.Add(unaryExpressionRepresentation.Operand);
                break;
            }

            return(result);
        }
        public InvocationExpressionRepresentation DeepCloneWithExpressionRepresentation(ExpressionRepresentationBase expressionRepresentation)
        {
            var result = new InvocationExpressionRepresentation(
                this.Type?.DeepClone(),
                expressionRepresentation,
                this.Arguments?.DeepClone());

            return(result);
        }