MemoryStream只能读写byte格式吗?
在下定义了一个类,里面是有long和string类型,
public class Person { public long Sno { get; set; } public string Name { get; set; } public string Age { get; set; } public string HomeTown { get; set; } public string Shool { get; set; } public string Country { get; set; } public string Language { get; set; } public string Professional { get; set; } public string Study { get; set; } public string FatherName { get; set; } public string MotherName { get; set; } }
然后初始化一个list来保存新建的100个person类信息,现在想将这list存入一个MemoryStream,然后定义StreamWrite和StreamReader读写,代码如下
static void Serializa(MemoryStream ms, List<Person> list) { //ms.Write( StreamWriter sw = new StreamWriter(ms); foreach (Person person in list) { PropertyInfo[] props = person.GetType().GetProperties(); foreach (PropertyInfo property in props) { sw.Write(property.Name); sw.Write(property.GetValue(person, null)); } } }
可是发现其实信息没有真的读写进MemoryStream,请教各位大声,我用的方法哪里出现问题了吗?
你最后要 sw.Close()
@Freedom0619: 不知道你想要什么结果
static void Serializa(MemoryStream ms, List<Person> list) { //ms.Write( StreamWriter sw = new StreamWriter(ms); foreach (Person person in list) { PropertyInfo[] props = person.GetType().GetProperties(); foreach (PropertyInfo property in props) { sw.Write(property.Name); sw.Write(property.GetValue(person, null)); } } sw.Close(); }
var ms = new MemoryStream(); Serializa(ms, new List<Person>{ new Person{ Age=12},new Person{ Age=132},new Person{ Age=162} });
从ms中可以读到这些到string中:
Sno0NameAge12HomeTownShoolCountryLanguageProfessionalStudyFatherNameMotherNameSno0NameAge132HomeTownShoolCountryLanguageProfessionalStudyFatherNameMotherNameSno0NameAge162HomeTownShoolCountryLanguageProfessionalStudyFatherNameMotherName
@Freedom0619: 你得重新设计设计
@Freedom0619: 在
static void deSerializa(MemoryStream dems) {
dems.Seek(0, SeekOrigin.Begin);
List<Person> NewList = new List<Person>(); StreamReader fsR = new StreamReader(dems); PropertyInfo[] fields = typeof(Person).GetProperties(); if (fsR.Peek() >= 0) { for (int i = 0; i < 100; i++) { Person person = new Person(); foreach (PropertyInfo field in fields) { if (field.Name == fsR.ReadLine()) { field.SetValue(person, fsR.ReadLine(), null); } } NewList.Add(person); } } } }
其他不用改
@Yu: 哇~~真的可以读到~~~可是person里面有个long类型~~~field.SetValue(person, fsR.ReadLine(), null);估计转换不成功~~~啊~~~又回到设置类的属性问题了~~~
@Freedom0619: 转换一下就可以了,或 public long Sno { get; set; } 改成
public string Sno { get; set; }
@Yu: 这是带我的人给我出的题目,就是让我解决这个问题~~~
@Freedom0619:
if (field.Name == fsR.ReadLine()) { if (field.PropertyType.FullName == "System.Int64") field.SetValue(person,Int64.Parse( fsR.ReadLine()), null); else field.SetValue(person, fsR.ReadLine(), null); }
@Yu: 哇~~大神你咋这么牛啊~~新手膜拜中~~~
流=字节流
啊~~~好纠结的啊~~~要把String 和long都转成byte才能存入吗?~~后面如何把字节流分别转成long和string又是个问题~~~~
要命这是我这样写居然不报错~~~
using( StreamWriter sw = new StreamWriter(ms)){ foreach (Person person in list) { PropertyInfo[] props = person.GetType().GetProperties(); foreach (PropertyInfo property in props) { sw.Write(property.Name); sw.Write(property.GetValue(person, null)); } } }
用json会好很多吧?
可以先序列化为字符串,再写入Stream