TryFinally() public static method

Creates a TryExpression representing a try block with a finally block and no catch statements.
public static TryFinally ( Expression body, Expression @finally ) : TryExpression
body Expression The body of the try block.
@finally Expression
return TryExpression
Ejemplo n.º 1
0
        public void TryFinally()
        {
            var expected = LinqExpression.TryFinally(
                LinqExpression.Constant(0L),
                LinqExpression.Constant(0L));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :tryBody _:zero ;
    :tryFinally _:zero ;
.
_:zero
    :constantValue 0 ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
        private static Expression Expand(TryExpression expression)
        {
            var res = expression.Body;

            var handlers = default(List <CatchBlock>);

            foreach (var handler in expression.Handlers)
            {
                if (handler.Filter != null)
                {
                    if (handlers != null)
                    {
                        res      = Expression.TryCatch(res, handlers.ToArray());
                        handlers = null;
                    }

                    res = Expression.TryCatch(res, handler);
                }
                else
                {
                    if (handlers == null)
                    {
                        handlers = new List <CatchBlock>();
                    }

                    handlers.Add(handler);
                }
            }

            if (handlers != null)
            {
                res = Expression.TryCatch(res, handlers.ToArray());
            }

            if (expression.Finally != null)
            {
                res = Expression.TryFinally(res, expression.Finally);
            }

            return(res);
        }
Ejemplo n.º 3
0
        private LambdaExpression createForCollection(Type from, Type to)
        {
            var cType = (from i in @from.GetTypeInfo().ImplementedInterfaces.Concat(new[] { @from })
                         where i.GetTypeInfo().IsInterface &&
                         i.GenericTypeArguments.Length == 1 &&
                         i.GetGenericTypeDefinition() == typeof(IReadOnlyCollection <>)
                         select i.GenericTypeArguments[0]).SingleOrDefault();

            if (cType == null)
            {
                return(null);
            }
            var toParameters  = to.GetTypeInfo().GenericTypeArguments;
            var input         = Ex.Parameter(from, "input");
            var getEnumerator = getGetEnumeratorMethod(from);
            var enumerator    = Ex.Parameter(getEnumerator.ReturnType, "enumerator");
            var eType         = getEnumerator.ReturnType.GetTypeInfo().GetDeclaredProperty(nameof(IEnumerator.Current)).PropertyType;
            var converters    = toParameters.Select(p => Ref.GetLambda(eType, p)).ToArray();

            var res        = toParameters.Select(p => Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p))).ToArray();
            var end        = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            Ex  conversion = enumeratorConversion(to, enumerator, eType, converters, res, end);
            var block      = Ex.Block(res.Concat(new[] { enumerator }),
                                      Ex.IfThen(
                                          Ex.MakeBinary(Et.LessThan,
                                                        Ex.Property(input, typeof(IReadOnlyCollection <>).MakeGenericType(cType).GetTypeInfo()
                                                                    .GetDeclaredProperty(nameof(IReadOnlyCollection <object> .Count))),
                                                        Ex.Constant(TupleArity(to))),
                                          Ex.Goto(end, NoResult(to))),
                                      Ex.Assign(enumerator, Ex.Call(input, getEnumerator)),
                                      typeof(IDisposable).GetTypeInfo().IsAssignableFrom(enumerator.Type.GetTypeInfo())
                    ? (Ex)Ex.TryFinally(conversion,
                                        Ex.Call(enumerator, typeof(IDisposable).GetTypeInfo().GetDeclaredMethod(nameof(IDisposable.Dispose))))
                    : conversion,
                                      Ex.Label(end, Result(to, Ex.Call(Creator(to), res.Select(r => Ex.Property(r, nameof(IConversionResult.Result)))))));

            var lambda = Ex.Lambda(block, input);

            return(lambda);
        }
Ejemplo n.º 4
0
        private LambdaExpression createDefault(Type from, Type to)
        {
            var toParameters  = to.GetTypeInfo().GenericTypeArguments;
            var input         = Ex.Parameter(from, "input");
            var getEnumerator = getGetEnumeratorMethod(from);
            var enumerator    = Ex.Parameter(getEnumerator.ReturnType, "enumerator");
            var eType         = getEnumerator.ReturnType.GetTypeInfo().GetDeclaredProperty(nameof(IEnumerator.Current)).PropertyType;
            var converters    = toParameters.Select(p => Ref.GetLambda(eType, p)).ToArray();

            var res        = toParameters.Select(p => Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p))).ToArray();
            var end        = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end");
            var conversion = enumeratorConversion(to, enumerator, eType, converters, res, end);
            var block      = Ex.Block(res.Concat(new[] { enumerator }),
                                      Ex.Assign(enumerator, Ex.Call(input, getEnumerator)),
                                      typeof(IDisposable).GetTypeInfo().IsAssignableFrom(enumerator.Type.GetTypeInfo())
                    ? (Ex)Ex.TryFinally(conversion,
                                        Ex.Call(enumerator, typeof(IDisposable).GetTypeInfo().GetDeclaredMethod(nameof(IDisposable.Dispose))))
                    : conversion,
                                      Ex.Label(end, Result(to, Ex.Call(Creator(to), res.Select(r => Ex.Property(r, nameof(IConversionResult.Result)))))));

            var lambda = Ex.Lambda(block, input);

            return(lambda);
        }
Ejemplo n.º 5
0
 public override SysExpr ToExpression() =>
 Finally == null?SysExpr.TryCatch(Body.ToExpression(), ToCatchBlocks(Handlers)) :
     Handlers == null?SysExpr.TryFinally(Body.ToExpression(), Finally.ToExpression()) :
         SysExpr.TryCatchFinally(Body.ToExpression(), Finally.ToExpression(), ToCatchBlocks(Handlers));