利用反射实现序列化和反序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Reflection;
using System.Xml;

namespace ProtoBuf
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = InitData();
            TestSerialize(list);
            Console.Read();
        }  

        static void TestSerialize(List<Person> list)
        {
            //定义测试次数和统计时间
            int count = 20;
            double time1 = 0,time2=0;
           
            //测试序列化
            Stopwatch PS = new Stopwatch();
            for (int i = 0; i < count; i++)
            {
                PS.Start();
                Serializa(list);
                PS.Stop();
                time1 += PS.Elapsed.TotalMilliseconds;
            }
            Console.WriteLine("序列化20次平均用时" + time1/count);

            //测试反序列化
            Stopwatch dePS = new Stopwatch();
            for (int j = 0; j < count; j++)
            {
                dePS.Start();
                deSerializa(list);
                dePS.Stop();
                time2 += dePS.Elapsed.TotalMilliseconds;
            }
            Console.WriteLine("反序列化20次平均用时" + time2/count);    
        }

        private static List<Person> InitData()
        {
            List<Person> list = new List<Person>();
            for (var i = 0; i < 100; i++)       //i < 1024*100;
            {
                var person = new Person
                {
                    Sno = i,
                    Name = "Name" + i,
                    Age = 20 + i,
                    HomeTown = "HomeTown" + i,
                    Shool = "Shool" + i,
                    Country = "Country" + i,
                    Language = "Language" + i,
                    Professional = "professional" + i,
                    Study = "study" + i,
                    FatherName = "fatherName" + i,
                    MotherName = "motherName" + i
                };
                list.Add(person);
            }
            return list;
        }
      
        //序列化
        static void Serializa(List<Person> list)
        {
            string[] a = new string[1000*1000];
            object[] b = new object[1000*1000];

            //新建文件流处理
            FileStream fs = new FileStream(@"E:\person.txt", FileMode.Create, FileAccess.Write, FileShare.None);
            StreamWriter sw = new StreamWriter(fs);

            //定义字典,利用反射获取类和属性
            IList<Dictionary<string, object>> Id = new List<Dictionary<string, object>>();
            foreach (Person person in list)
            {
                PropertyInfo[] props = person.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
                Dictionary<string,object> values = new Dictionary<string, object>();
                foreach (PropertyInfo property in props)
                {
                    values.Add(property.Name, property.GetValue(person, property.GetIndexParameters())); 
                    sw.WriteLine(property.Name);
                    sw.WriteLine(property.GetValue(person, property.GetIndexParameters()).ToString());
                }
                Id.Add(values);   
            }
            fs.Close();
            fs.Dispose();
        }
       
        //反序列化
        static void deSerializa(List<Person> list)
        {
            int j = 0;
            string[] a=new string[1000*1000];

            //新建流文件,读取数据
            FileStream fsRead = new FileStream(@"E:\person.txt", FileMode.Open, FileAccess.Read, FileShare.None);
            StreamReader fsR=new StreamReader(fsRead);
            fsR.BaseStream.Seek(0, SeekOrigin.Begin);
            string str=fsR.ReadLine();
            while(str!=null)
            {
                a[j] = str;
                j++;
                //Console.WriteLine("{0}", str);
                str = fsR.ReadLine();
            }

            //新建person类
            List<Person> list2 = new List<Person>();
            for (var i = 0; i < 100; i++)       //i < 1024*100;
            {
                var person = new Person
                {
                    Sno =Convert.ToInt32(a[1+22*i]),
                    Name = a[3 + 22 * i],
                    Age = Convert.ToInt32(a[5 + 22 * i]),
                    HomeTown = a[7 + 22 * i],
                    Shool = a[9 + 22 * i],
                    Country = a[11 + 22 * i],
                    Language = a[13 + 22 * i],
                    Professional = a[15 + 2 * i],
                    Study = a[17 + 22 * i],
                    FatherName = a[19 + 22 * i],
                    MotherName = a[21 + 22 * i],
                };
                list.Add(person);
            }
            fsRead.Close();
            fsRead.Dispose();
        }
    }

    public class Person
    {
        public int Sno { get; set; }
        public string Name { get; set; }
        public int 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; }
    }
}

 

posted on 2014-12-01 16:33  Freedom0619  阅读(192)  评论(0编辑  收藏  举报

导航