public override void GenerateDeserializeIL(MethodGenerator g) { SetMember(g, () => { var count = g.Declare <int>(); g.Assign(count, () => { SerializationMethodHelper.ReadLength(g, this.DTOMemberName);//读取数量 }); var list = g.Declare(this.TargetType); g.If(() => { g.Load(count); g.Load(0); return(LogicOperator.LessThan); }, () => { //数量小于1 //list = new List<T>(); var elementType = this.TargetType.ResolveElementType(); g.Assign(list, () => { g.NewObject(this.TargetType); }); }, () => { //list = new List<T>(); g.Assign(list, () => { g.NewObject(this.TargetType); }); var elementType = this.TargetType.ResolveElementType(); g.For(count, (index) => { var item = g.Declare(elementType); g.Assign(item, () => { SerializationMethodHelper.ReadElement(g, this.DTOMemberName, elementType, index); }); g.Load(list); g.Load(item); g.Call(this.TargetType.ResolveMethod("Add", elementType)); }); }); g.Load(list); }); }
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); }
public override void GenerateDeserializeIL(MethodGenerator g) { SetMember(g, () => { var array = g.Declare(this.TargetType); if (this.MemberAttribute.IsBlob) { g.Assign(array, () => { SerializationMethodHelper.ReadBlob(g, this.DTOMemberName);//读取数量 }); } else { var length = g.Declare <int>(); g.Assign(length, () => { SerializationMethodHelper.ReadLength(g, this.DTOMemberName);//读取数量 }); g.If(() => { g.Load(length); g.Load(0); return(LogicOperator.LessThan); }, () => { //数量小于1 //array = new array[]; var elementType = this.TargetType.ResolveElementType(); g.Assign(array, () => { g.NewArray(elementType, () => { g.Load(length); }); }); }, () => { var elementType = this.TargetType.ResolveElementType(); //int[] = new int[c]; g.Assign(array, () => { g.NewArray(elementType, () => { g.Load(length); }); }); g.For(length, (index) => { var item = g.Declare(elementType); g.Assign(item, () => { SerializationMethodHelper.ReadElement(g, this.DTOMemberName, elementType, index); }); g.StoreElement(array, index, item); }); }); } g.Load(array); }); }