コード例 #1
0
        /// <summary>
        /// Produces an <see cref="Expression"/> which represents an instance creation of passed <paramref name="targetType"/> and assignment to a local variable.
        /// </summary>
        /// <param name="targetType">The type which creation should be built to <see cref="Expression"/>.</param>
        /// <param name="referencedVariableExpression">When returns true, <paramref name="referencedVariableExpression"/> contains expression which refers to the newly created instance, otherwise null.</param>
        /// <returns>The <see cref="Expression"/> containing the assignment and creation of type.</returns>
        public BinaryExpression BuildInstanceCreationWithAssigmentExpression(Type targetType, out ParameterExpression referencedVariableExpression)
        {
            var createInstanceExpr = CreateInstanceCreationExpression(targetType);
            var varName            = targetType.Name.ToCamelCase();

            if (!identifierValidator.IsValidIdentifier(varName))
            {
                varName = "@" + varName;
            }
            referencedVariableExpression = Expression.Variable(targetType, varName);
            var assignExpr = Expression.Assign(referencedVariableExpression, createInstanceExpr);

            return(assignExpr);
        }