Пример #1
0
        public void WhileBreakContinue()
        {
            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.While(
                    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);
        }
        public WhileExpression 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.While(test, body, breakTarget, continueTarget));
        }
Пример #3
0
        public void WhileFalse()
        {
            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.While(
                    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(0, counter.Count);
        }
Пример #4
0
        public void While()
        {
            var i = Expression.Parameter(typeof(int), "i");
            var l = Expression.Parameter(typeof(int), "l");

            var lambda = Expression.Lambda <Action <int, int> > (
                CustomExpression.While(
                    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)
{
	while (i < l)
	{
		Console.WriteLine(i++);
	}
}
", lambda);
        }