学习笔记-Serialization

博客介绍了对象序列化相关知识,包括可序列化对象的标记方式,如使用[Serializable];还提及使属性不被序列化的方法,用[NonSerialized]标记。同时给出二进制流序列化、反序列化示例,以及实现IFormater接口和xml序列化的要点,还提到加密解密序列化数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1。什么样的对象可以被序列化
标记[Serializable]public class MyClass {}
怎样不使属性不被序列化呢?
[NonSerialized] int _cashSize;
标记变量或者说成员
//二进制流序列化
ArrayList l = new ArrayList();
for (int x=0; x< 100; x++)
{ l.Add (x); }
FileStream s = File.Create("foo.bin");
BinaryFormatter b = new BinaryFormatter();
b.Serialize(s, l);
s.Close();
//反序列化
FileStream s = File.OpenRead("foo.bin");
BinaryFormatter b = new BinaryFormatter(); 
ArrayList p = (ArrayList) b.Deserialize(s);
s.Close();
PrintValues(p);

//实现IFormater接口
IFormater formater = new BinaryFormater();
IFormater foramter = new SoapFormater();


//加密解密序列化的数据System.Security.Cryptography

xml序列化只能序列public成员
且序列化需要传入typeof(要序列化的对象)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值