我的MemoryStream中有这些内容~~~
现在想传到List<Person>里面~~~~我代码是这样写的
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Reflection; namespace 用户自定义输入 { public class DeSerializerHelper { public static void DeSerializa<T>(List<T> list, MemoryStream ms,T obj) { ms.Seek(0, SeekOrigin.Begin); BinaryReader br = new BinaryReader(ms); PropertyInfo[] fields = typeof(T).GetProperties(); for (int i = 0; i <= 8; i++) { foreach (PropertyInfo field in fields) { if (field.Name == br.ReadString()) { if (field.PropertyType.FullName == "System.Int64") field.SetValue(obj, Int64.Parse(br.ReadString()), null); else field.SetValue(obj, br.ReadString(), null); } } } list.Add(obj); } } } }
可是存到list里面是这个样子的,希望有感觉都输出了最后一个person~~~希望有高手能指点一二,万分感谢~~~
上次还没结贴呢
肯定这样了,你传的obj,大家都共用一个内存地址,你要在循环里面创建
@Freedom0619:
for (int i = 0; i <= 8; i++) { T obj = Activator.CreateInstance<T>(); ... }