MakeTry() public static method

Creates a TryExpression representing a try block with the specified elements.
public static MakeTry ( Type type, Expression body, Expression @finally, Expression fault, IEnumerable handlers ) : TryExpression
type Type The result type of the try expression. If null, body and all handlers must have identical type.
body Expression The body of the try block.
@finally Expression
fault Expression The body of the t block. Pass null if the try block has no fault block associated with it.
handlers IEnumerable A collection of s representing the catch statements to be associated with the try block.
return TryExpression
Example #1
0
 /// <summary>
 /// Creates a new expression that is like this one, but using the
 /// supplied children. If all of the children are the same, it will
 /// return this expression.
 /// </summary>
 /// <param name="body">The <see cref="Body" /> property of the result.</param>
 /// <param name="handlers">The <see cref="Handlers" /> property of the result.</param>
 /// <param name="finally">The <see cref="Finally" /> property of the result.</param>
 /// <param name="fault">The <see cref="Fault" /> property of the result.</param>
 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
 public TryExpression Update(Expression body, IEnumerable <CatchBlock> handlers, Expression @finally, Expression fault)
 {
     if (body == Body && handlers == Handlers && @finally == Finally && fault == Fault)
     {
         return(this);
     }
     return(Expression.MakeTry(Type, body, @finally, fault, handlers));
 }
        public static TryExpression CreateTryExpression(Type type, Expression body, Expression @finally, Expression fault, ReadOnlyCollection <CatchBlock> handlers)
        {
#if LINQ
            return(new TryExpression(type, body, @finally, fault, handlers));
#else
            return(Expression.MakeTry(type, body, @finally, fault, handlers));
#endif
        }
 public TryExpression Update(Expression body, IEnumerable <CatchBlock> handlers, Expression @finally, Expression fault)
 {
     if (((body == this.Body) && (handlers == this.Handlers)) && ((@finally == this.Finally) && (fault == this.Fault)))
     {
         return(this);
     }
     return(Expression.MakeTry(this.Type, body, @finally, fault, handlers));
 }
Example #4
0
        protected internal virtual Expression VisitTry(TryExpression node)
        {
            Expression b = Visit(node.Body);
            ReadOnlyCollection <CatchBlock> h = Visit(node.Handlers, VisitCatchBlock);
            Expression y = Visit(node.Finally);
            Expression f = Visit(node.Fault);

            if (b == node.Body &&
                h == node.Handlers &&
                y == node.Finally &&
                f == node.Fault)
            {
                return(node);
            }
            return(Expression.MakeTry(b, y, f, h));
        }
        // NB: We never take out empty try blocks that have a finally or fault handler, because
        //     these handlers provide the guarantee of not being interrupted by asynchronous
        //     exceptions and are often used for concurrency-safe programming.

        /// <summary>
        /// Visits a try expression to perform optimization steps.
        /// </summary>
        /// <param name="node">The try expression to visit.</param>
        /// <returns>The result of optimizing the try expression.</returns>
        protected override Expression VisitTry(TryExpression node)
        {
            var res = (TryExpression)base.VisitTry(node);

            AssertTypes(node, res);

            var opt = SimplifyTry(res);

            AssertTypes(node, opt);

            if (opt.NodeType != ExpressionType.Try)
            {
                return(opt);
            }

            var optimizedTry = (TryExpression)opt;

            var body     = optimizedTry.Body;
            var @finally = optimizedTry.Finally;

            if (IsPure(body))
            {
                if (@finally == null || HasConstantValue(@finally))
                {
                    return(ChangeType(body, optimizedTry.Type));
                }
                else
                {
                    return(Expression.MakeTry(optimizedTry.Type, body, @finally, fault: null, handlers: null));
                }
            }
            else if (AlwaysThrows(body))
            {
                // NB: We can possibly evaluate pure catch blocks ahead of time if we know the
                //     body throws. However, we'd need to know the exact runtime type of the
                //     exception being thrown.

                return(EvaluateTryThrow(optimizedTry));
            }

            return(optimizedTry);
        }