public void UsingException() { var disposable = new Disposable(); var d = Expression.Parameter(typeof(Disposable), "d"); var disposer = Expression.Lambda <Action <Disposable> > ( CustomExpression.Using( d, Expression.Block( Expression.Throw(Expression.New(typeof(TestUsingException))), Expression.Call(d, typeof(Disposable).GetMethod("Touch")))), d).Compile(); Assert.IsFalse(disposable.Touched); Assert.IsFalse(disposable.Disposed); try { disposer(disposable); Assert.Fail(); } catch (TestUsingException) {} Assert.IsFalse(disposable.Touched); Assert.IsTrue(disposable.Disposed); }
public UsingExpression Update(ParameterExpression variable, Expression disposable, Expression body) { if (this.variable == variable && this.disposable == disposable && this.body == body) { return(this); } return(CustomExpression.Using(variable, disposable, body)); }
public void Using() { var arg = Expression.Parameter(typeof(IDisposable), "arg"); var lambda = Expression.Lambda <Action <IDisposable> > ( CustomExpression.Using( arg, Expression.Call(typeof(Console).GetMethod("WriteLine", new [] { typeof(object) }), arg)), arg); AssertExpression(@" void (IDisposable arg) { using (arg) { Console.WriteLine(arg); } } ", lambda); }
public void Using() { var disposable = new Disposable(); var d = Expression.Parameter(typeof(Disposable), "d"); var disposer = Expression.Lambda <Action <Disposable> > ( CustomExpression.Using( d, Expression.Block( Expression.Call(d, typeof(Disposable).GetMethod("Touch")))), d).Compile(); Assert.IsFalse(disposable.Touched); Assert.IsFalse(disposable.Disposed); disposer(disposable); Assert.IsTrue(disposable.Touched); Assert.IsTrue(disposable.Disposed); }