自定义序列化(obj to bytes [])和反序列化(bytes []到object)

时间:2013-05-30 06:05:11

标签: c#

我正在进行自定义序列化和反序列化。

这是我的序列化代码:

//customSampleData is my class
static void SerializeCustom(customSampleData csd)
        {
            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write(csd.FName);
            bw.Write(csd.MName);
            bw.Write(csd.LName);
            bw.Write(csd.Age);
            bw.Write(csd.Address);
            bw.Write(csd.Religion);
            bw.Write(csd.BDay.ToString());
            bw.Write(csd.Sex);
            bw.Write(csd.CStatus);

            byte[] byteform = ms.ToArray();

            bw.Flush();
            bw.Close();
            DeserializeCustom(byteform);

        }

这是我的反序列化代码:

static void DeserializeCustom(Byte[] b)
{
    object obj;
    customSampleData csd = new customSampleData();
    MemoryStream ms = new MemoryStream(b);
    BinaryReader br = new BinaryReader(ms);
    csd.FName = br.ReadString();
    csd.MName = br.ReadString();
    csd.LName = br.ReadString();
    csd.Age = br.ReadInt32();
    csd.Address = br.ReadString();
    csd.Religion = br.ReadString(); ;
    csd.BDay = DateTime.Parse(br.ReadString());
    csd.Sex = br.ReadString();
    csd.CStatus = br.ReadString();

    ms.Flush();
    ms.Close();
}

请帮助我以“自定义”方式进行DESERIALIZE。

谢谢!

2 个答案:

答案 0 :(得分:0)

[Serializable]
class customSampleData  : ISerializable, IDeserializationCallback
{
    public string FName
    {
        get;
        private set;
    }

    public string MName
    {
        get;
        private set;
    }

    public string LName
    {
        get;
        private set;
    }

    public string Age
    {
        get;
        private set;
    }

    public string Address
    {
        get;
        private set;
    }

    public string Religion
    {
        get;
        private set;
    }

    public DateTime BDay
    {
        get;
        private set;
    }

    public string Sex
    {
        get;
        private set;
    }
    public string CStatus
    {
        get;
        private set;
    }

    public customSampleData (string pFName, string pMName, string pLName, string pAge, string pAddress, string pReligion, DateTime pBDay, string pSex, string pCStatus)
    {
        FName = pFName;
        MName = pMName;
        LName = pLName;
        Age = pAge;
        Address = pAddress;
        Religion =pReligion;
        BDay =pBDay;
        Sex = pSex;
        CStatus = pCStatus;
    }

    public customSampleData(SerializationInfo info, StreamingContext context)
    {
        // Deserialization Constructor 

        FName = info.GetString("FName");
        MName = info.GetString("MName");
        LName = info.GetString("LName");
        Age = info.GetString("Age");
        Address = info.GetString("Address");
        Religion = info.GetString("Religion");
        BDay = Convert.ToDateTime(info.GetString("BDay"));
        Sex = info.GetString("Sex");
        CStatus = info.GetString("CStatus");
        Console.WriteLine("Deserializing constructor");
    }



    [OnSerializing]
    private void OnSerializing(StreamingContext context)
    {
        Console.WriteLine("OnSerializing fired.");
    }

    [OnSerialized]
    private void OnSerialized(StreamingContext context)
    {
        Console.WriteLine("OnSerialized fired.");
    }

    [OnDeserializing]
    private void OnDeserializing(StreamingContext context)
    {
        Console.WriteLine("OnDeserializing fired.");
    }

    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
        Console.WriteLine("OnDeserialized fired.");
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        Console.WriteLine("Serializing...");
        info.AddValue("FName", FName);
        info.AddValue("MName", MName);
        info.AddValue("LName", LName);
        info.AddValue("Age", Age);
        info.AddValue("Address", Address);
        info.AddValue("Religion", Religion);
        info.AddValue("BDay", BDay);
        info.AddValue("Sex", Sex);
        info.AddValue("CStatus", CStatus);

    }

    void IDeserializationCallback.OnDeserialization(object sender)
    {
        Console.WriteLine("IDeserializationCallback.OnDeserialization method.");
    }
}


   customSampleData tc = new customSampleData("fname", "mname", "pLName", "pAge", "pAddress", "pReligion", DateTime.Now, "pSex", "pCStatus");
        FileStream fs = new FileStream("Serialized.txt", FileMode.Create);
        BinaryFormatter bf = new BinaryFormatter();

        bf.Serialize(fs, tc);
        fs.Close();
        tc = null;

        fs = new FileStream("Serialized.txt", FileMode.Open);

        tc = (customSampleData)bf.Deserialize(fs);

        Console.WriteLine("fname = " + tc.FName);

输出将为output

答案 1 :(得分:0)

要从函数返回结果,您需要指定返回类型并在结尾添加return语句:

static CustomSampleData DeserializeCustom(Byte[] b)
{
    var csd = new CustomSampleData();
    using(var ms = new MemoryStream(b))
    {
      using (var br = new BinaryReader(ms))
      {
        csd.FName = br.ReadString();
        ...
      }
    }
    return csd;
}

注意:

  • using负责正确关闭流/作家。
  • 在C#中,类的默认命名约定是UpperCase