2007-8-7


6号玩游戏忘了时间,计划加到7号。。
ASP.NET2.0利用MemberShip可更方便进行用户验证
懂得重载方法,却没想到重载结构的构造函数public tStruct(){}  public tStruct( x, xx ){ m_x=x , m_xx=xx }
使用 Serializable属性对类进行标记可使类可序列化,使用NonSerialized标记成员变量防止被序列化

[Serializable]
public class ClassFormatter
{    public int n1 = 0;
    public int n2 = 0;
    [NonSerialized]  public int n3 = 0;
    public string str = null;
 
    public ClassFormatter() {}

    public ClassFormatter(int i1,int i2,string str)
    {   this.n1 = i1;
        this.n2 = i2;
        this.str = str;  
 }}
//序列化与反序列化
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

     //序列化,使用BinaryFormatter是因为效率很高,但如果要具有移植性,还是使用SoapFormatter转XML
        ClassFormatter cf = new ClassFormatter(1,24,"Test");
        //ClassFormatter cf = new ClassFormatter();
        //cf.n1 = 1;
        //cf.n2 = 24;
        //cf.str = "Test";
        IFormatter formatter = new BinaryFormatter();
        Stream stm = new FileStream("Test.Bin",FileMode.Create,FileAccess.Write,FileShare.None);
        formatter.Serialize(stm,cf);
        stm.Close();
    //反序列化     出于性能方面的考虑, 对对象进行反序列化时并不调用构造函数!
        IFormatter formatter = new BinaryFormatter();
        Stream stm = new FileStream("Test.Bim",FileMode.Open,FileAccess.Read,FileShare.Read);
        ClassFormatter cf = (ClassFormatter)formatter.Deserialize(stm);
        stm.Close();
        Console.WriteLine("n1: {0}", cf.n1);
        Console.WriteLine("n2: {0}", cf.n2);
        Console.WriteLine("str: {0}", cf.tr);
无法继承 Serializable 属性。如果从 MyObject 派生出一个新的类,则这个新的类也必须使用该属性进行标记,否则将无法序列化

public class myStuff : ClassFormatter
{    public int n3;}
编辑成功,但试图序列化时,抛出SerializationException,说明 MyStuff 类型未标记为可序列化。
通过在对象上实现 ISerializable 接口来自定义序列化过程,在反序列化后成员变量的值失效时尤其有用,但需要为变量提供值以重建对象的完整状态。
实现 ISerializable,需实现 GetObjectData 方法以及一个特殊的构造函数,在反序列化对象时要用到此构造函数 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值