コード例 #1
0
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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);
        }