• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c#对象的序列化

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


//本程序演示了最简单的序列化 和反序列化 ,将对象存储到本地文件中 ,
//对象序列化的另一个目的是网络传输信息
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;
namespace 序列化_啊
{

    class Program
    {
        static void Main(string[] args)
        {
            //创建文件a.dat
            FileStream f = new FileStream("a.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
            //创建类的实例t
            test t = new test();
            //给属性赋值
            t.A = "hello";
            t.N = 5;

            //二进制序列化
            IFormatter format = new BinaryFormatter();
            format.Serialize(f, t);
            f.Close();

            //读取文件
            f = new FileStream("a.dat", FileMode.Open, FileAccess.Read);
            test m = (test)format.Deserialize(f);
            Console.WriteLine(m.A);
            Console.WriteLine(m.N);
            f.Close();
           
            Console.Read();
        }

       [Serializable]//可序列化标记
        class test
        {
            int n = 0;
            string a = null;
            [NonSerialized]//不可序列化标记
            int b = 10;
            public int N
            {
                set
                {
                    n = value;
                }
                get
                {
                    return n;
                }
            }
            public string A
            {
                set
                {
                    a = value;
                }
                get
                {
                    return a;
                }
            }
        }
    }
}

阅读全文
类别:默认分类 查看评论


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
SAD算法在opencv上的实现代码(c++)发布时间:2022-07-14
下一篇:
C#调用百度地图API经验分享(一)发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap