public void If_Condition_And() { DynamicMethod method = new DynamicMethod("temp", typeof(int), Type.EmptyTypes); MethodGenerator g = new MethodGenerator(method); var a = g.Declare(5); g.If(() => {//a<10 && a>6 g.And(() => { g.Load(a); g.Load(10); return(LogicOperator.LessThan); }, () => { g.Load(a); g.Load(6); return(LogicOperator.GreaterThan); }); return(LogicOperator.IsTrue); }, () => { g.Increment(a); }); g.Load(a); g.Return(); int result = (int)method.Invoke(null, new object[] { }); Assert.AreEqual(5, result); }
public void While() { DynamicMethod method = new DynamicMethod("temp", typeof(int), Type.EmptyTypes); MethodGenerator g = new MethodGenerator(method); const int maxNumber = 15; var num = g.Declare(0); var max = g.Declare(maxNumber); g.While(() => { g.Load(num); g.Load(max); return(LogicOperator.LessThan); }, () => { g.Increment(num); }); g.Load(num); g.Return(); int result = (int)method.Invoke(null, new object[] { }); Assert.AreEqual(maxNumber, result); }