Ejemplo n.º 1
0
        public void For()
        {
            DynamicMethod   method = new DynamicMethod("temp", typeof(int), new Type[] { typeof(List <int>) });
            MethodGenerator g      = new MethodGenerator(method);

            IVariable array = g.Declare <List <int> >();

            g.Assign(array, () =>
            {
                g.LoadParameter(0);
            });

            var count  = g.Declare <int>();
            var number = g.Declare(0);

            g.Assign(count, () => {
                g.LoadParameter(0);
                g.LoadMember("Count");
            });

            g.For(count, (index) =>
            {
                g.Assign(number, () =>
                {
                    g.LoadParameter(0);
                    g.Load(index);
                    g.Call(typeof(List <int>).ResolveMethod("get_Item", typeof(int)));
                    g.Load(number);
                    g.Add <int>();
                });
            });

            g.Load(number);

            g.Return();

            List <int> list = new List <int> {
                1, 2, 3, 4, 5
            };
            var result = (int)method.Invoke(null, new object[] { list });

            Assert.AreEqual(15, result);
        }