C#序列化和反序列化

 

[Serializable]
    public class RoleInfo
    {
        public int Id
        {
            set;
            get;
        }
        public int Level
        {
            set;
            get;
        }
        public string Name
        {
            set;
            get;
        }
    }
public void Serialize(RoleInfo info)
{
        FileStream fs = new FileStream(savePath, FileMode.Create);//savePath:保存到本地的路径
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(fs, info);
        fs.Close();
}

public void Deserialize()
{
        FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Read, FileShare.Read);
        BinaryFormatter bf = new BinaryFormatter();
        RoleInfo info= bf.Deserialize(fs) as RoleInfo;
}

 

posted @ 2013-02-18 14:04  Marble  阅读(162)  评论(0编辑  收藏