public void DoWhileBreakContinue()
        {
            var counter = new Counter();

            var c = Expression.Parameter(typeof(Counter), "c");
            var i = Expression.Parameter(typeof(int), "i");
            var l = Expression.Parameter(typeof(int), "l");

            var loop_break    = Expression.Label("for_break");
            var loop_continue = Expression.Label("for_continue");

            var hitcounter = Expression.Lambda <Action <Counter, int, int> > (
                CustomExpression.DoWhile(
                    Expression.LessThan(i, l),
                    Expression.Block(
                        Expression.Condition(
                            Expression.LessThan(i, Expression.Constant(10)),
                            Expression.Block(
                                Expression.Call(c, typeof(Counter).GetMethod("Hit", Type.EmptyTypes)),
                                Expression.PostIncrementAssign(i),
                                Expression.Goto(loop_continue)),
                            Expression.Goto(loop_break))),
                    loop_break,
                    loop_continue),
                c, i, l).Compile();

            hitcounter(counter, 0, 100);

            Assert.AreEqual(10, counter.Count);
        }
Beispiel #2
0
        public DoWhileExpression Update(Expression test, Expression body, LabelTarget breakTarget, LabelTarget continueTarget)
        {
            if (this.test == test && this.body == body && this.break_target == breakTarget && this.continue_target == continueTarget)
            {
                return(this);
            }

            return(CustomExpression.DoWhile(test, body, breakTarget, continueTarget));
        }
        public void DoWhileFalse()
        {
            var counter = new Counter();

            var c = Expression.Parameter(typeof(Counter), "c");
            var i = Expression.Parameter(typeof(int), "i");
            var l = Expression.Parameter(typeof(int), "l");

            var hitcounter = Expression.Lambda <Action <Counter, int, int> > (
                CustomExpression.DoWhile(
                    Expression.LessThan(i, l),
                    Expression.Block(
                        Expression.Call(c, typeof(Counter).GetMethod("Hit", Type.EmptyTypes)),
                        Expression.PreIncrementAssign(i))
                    ),
                c, i, l).Compile();

            hitcounter(counter, 100, 10);

            Assert.AreEqual(1, counter.Count);
        }
Beispiel #4
0
        public void DoWhile()
        {
            var i = Expression.Parameter(typeof(int), "i");
            var l = Expression.Parameter(typeof(int), "l");

            var lambda = Expression.Lambda <Action <int, int> > (
                CustomExpression.DoWhile(
                    Expression.LessThan(i, l),
                    Expression.Block(
                        Expression.Call(typeof(Console).GetMethod("WriteLine", new [] { typeof(int) }), Expression.PostIncrementAssign(i)))),
                i, l);

            AssertExpression(@"
void (int i, int l)
{
	do
	{
		Console.WriteLine(i++);
	}
	while (i < l);
}
", lambda);
        }