System.Linq.Expressions.Expression.MakeTry C# (CSharp) Method

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
        public static TryExpression MakeTry(Type type, Expression body, Expression @finally, Expression fault, IEnumerable<CatchBlock> handlers)
        {
            RequiresCanRead(body, nameof(body));

            ReadOnlyCollection<CatchBlock> @catch = handlers.ToReadOnly();
            ContractUtils.RequiresNotNullItems(@catch, nameof(handlers));
            ValidateTryAndCatchHaveSameType(type, body, @catch);

            if (fault != null)
            {
                if (@finally != null || @catch.Count > 0)
                {
                    throw Error.FaultCannotHaveCatchOrFinally(nameof(fault));
                }
                RequiresCanRead(fault, nameof(fault));
            }
            else if (@finally != null)
            {
                RequiresCanRead(@finally, nameof(@finally));
            }
            else if (@catch.Count == 0)
            {
                throw Error.TryMustHaveCatchFinallyOrFault();
            }

            return new TryExpression(type ?? body.Type, body, @finally, fault, @catch);
        }

Usage Example

Ejemplo n.º 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));
 }
All Usage Examples Of System.Linq.Expressions.Expression::MakeTry
Expression