首页 新闻 会员 周边 捐助

MemoryStream 读写问题

0
悬赏园豆:30 [已解决问题] 解决于 2014-12-05 09:22

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,请教各位大声,我用的方法哪里出现问题了吗?

Freedom0619的主页 Freedom0619 | 初学一级 | 园豆:10
提问于:2014-12-03 12:57
< >
分享
最佳答案
1

你最后要 sw.Close()

收获园豆:30
Yu | 专家六级 |园豆:12990 | 2014-12-03 13:08

@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

Yu | 园豆:12990 (专家六级) | 2014-12-03 13:41

@Freedom0619: 你得重新设计设计

Yu | 园豆:12990 (专家六级) | 2014-12-03 14:07

@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 | 园豆:12990 (专家六级) | 2014-12-03 14:24

@Yu: 哇~~真的可以读到~~~可是person里面有个long类型~~~field.SetValue(person, fsR.ReadLine(), null);估计转换不成功~~~啊~~~又回到设置类的属性问题了~~~

Freedom0619 | 园豆:10 (初学一级) | 2014-12-03 14:27

@Freedom0619: 转换一下就可以了,或 public long Sno { get; set; }  改成 

 

public string Sno { get; set; }

Yu | 园豆:12990 (专家六级) | 2014-12-03 14:30

@Yu: 这是带我的人给我出的题目,就是让我解决这个问题~~~

Freedom0619 | 园豆:10 (初学一级) | 2014-12-03 14:32

@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 | 园豆:12990 (专家六级) | 2014-12-03 14:34

@Yu: 哇~~大神你咋这么牛啊~~新手膜拜中~~~

Freedom0619 | 园豆:10 (初学一级) | 2014-12-03 14:36
其他回答(4)
0

流=字节流

吴瑞祥 | 园豆:29449 (高人七级) | 2014-12-03 12:58

啊~~~好纠结的啊~~~要把String 和long都转成byte才能存入吗?~~后面如何把字节流分别转成long和string又是个问题~~~~

支持(0) 反对(0) Freedom0619 | 园豆:10 (初学一级) | 2014-12-03 13:01

要命这是我这样写居然不报错~~~

支持(0) 反对(0) Freedom0619 | 园豆:10 (初学一级) | 2014-12-03 13:02
0
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));
                }
            }
}
Halower | 园豆:1723 (小虾三级) | 2014-12-03 13:01
0

用json会好很多吧?

XiaoFaye | 园豆:3087 (老鸟四级) | 2014-12-03 13:45
0

可以先序列化为字符串,再写入Stream

雲霏霏 | 园豆:238 (菜鸟二级) | 2014-12-03 15:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册