public override void GenerateSerializeIL(MethodGenerator g) { g.If(() => { LoadMemberValue(g);//加载集合到堆栈上,检查是否为null return(LogicOperator.IsNull); }, () => { SerializationMethodHelper.WriteArray(g, this.DTOMemberName); }, () => { var elementType = this.TargetType.ResolveElementType(); ////写入数组 SerializationMethodHelper.WriteArray(g, this.DTOMemberName); //写入每个项 LoadMemberValue(g); g.ForEach(item => { SerializationMethodHelper.WriteElement(g, this.DTOMemberName, elementType, () => { g.Load(item); }); }); }); }
public void Foreach() { DynamicMethod method = new DynamicMethod("temp", typeof(int), new Type[] { typeof(List <int>) }); MethodGenerator g = new MethodGenerator(method); List <int> list = new List <int> { 1, 2, 3, 4, 5 }; g.LoadParameter(0); var count = g.Declare <int>(); g.ForEach(item => { g.Load(item); g.Store(count); }); g.Load(count); g.Return(); var result = (int)method.Invoke(null, new object[] { list }); Assert.AreEqual(5, result); }