序列化问题

时间:2009-06-19 12:08:45

标签: c# .net serialization

我有序列化的问题。 我想将一个对象转换为一个字符串,反之亦然。 我有两种实用方法:

public
static byte[] Serialize(Object o)
{
   MemoryStream ms = new MemoryStream();
   BinaryFormatter bf1 = new BinaryFormatter();
   bf1.Serialize(ms, o);
   byte[] buffer = ms.ToArray();
   //string retStr = Convert.ToBase64String(buffer);
   return buffer;
}

public static object Deserialize(byte[] TheByteArray)
{
   //byte[] TheByteArray = Convert.FromBase64String(ParamStr);
   MemoryStream ms = new MemoryStream(TheByteArray);
   BinaryFormatter bf1 = new BinaryFormatter();
   ms.Position = 0;
   return bf1.Deserialize(ms);
}

我的测试代码是:

Student obj = new Student ();
obj.UserName = "Admin";
obj.Password = "Password";
obj.lessonIds = new int[] { 1, 2, 3, 4, 5 };
obj.lessonNames= new string[] { "Spanish", "Maths" };
obj.Id= 43;
byte[] retByteArray = Crypto.Serialize(obj);

Student objNew = new Student ();
objNew = (Student )Crypto.Deserialize(retByteArray);

此代码不起作用。 错误消息是:调用的目标抛出了异常。 解析完成之前遇到的流结束。

结束我的主要目标是将对象转换为字符串,但我甚至无法将其转换为字节数组

3 个答案:

答案 0 :(得分:1)

我刚刚尝试过原始代码并且工作正常但是,您需要确保将学生班级定义标记为[Serializable]

[Serializable]
public class Student
{
    public string UserName;
    public string Password;
    public int[] lessonIds;
    public string[] lessonNames;
    public int Id;
    public Student() { }
}

答案 1 :(得分:0)

您可能正在遭遇竞争条件,因为您在完成序列化时没有关闭内存流或格式化程序。

试试这个:

public
static byte[] Serialize(Object o)
{
   using (MemoryStream ms = new MemoryStream())
   {
       BinaryFormatter bf1 = new BinaryFormatter();
       bf1.Serialize(ms, o);
       byte[] buffer = ms.ToArray();
       //string retStr = Convert.ToBase64String(buffer);
   }
   return buffer;
}

public static object Deserialize(byte[] TheByteArray)
{
   //byte[] TheByteArray = Convert.FromBase64String(ParamStr);
   using (MemoryStream ms = new MemoryStream(TheByteArray))
   {
       BinaryFormatter bf1 = new BinaryFormatter();
       ms.Position = 0;
       var result = bf1.Deserialize(ms);
   }
   return result;
}

答案 2 :(得分:0)

该代码对我来说非常合适(我只是添加了缺少的Student类。该代码是否真正代表了真正的代码。特别是缓冲区处理(可能是文件IO)是我的第一个怀疑。但是这个错误不在你发布的代码中......它运行正常。

顺便说一句...... BinaryFormatter可能很脆弱,特别是在不同的版本中 - 您可能需要考虑替代序列化程序。如果您有兴趣,请询问更多信息。

这里可以运行:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System;
static class Crypto
{
    static byte[] Serialize(object o)
    {
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf1 = new BinaryFormatter();
        bf1.Serialize(ms, o);
        byte[] buffer = ms.ToArray();
        //string retStr = Convert.ToBase64String(buffer);
        return buffer;
    }

    public static object Deserialize(byte[] TheByteArray)
    {
        //byte[] TheByteArray = Convert.FromBase64String(ParamStr);
        MemoryStream ms = new MemoryStream(TheByteArray);
        BinaryFormatter bf1 = new BinaryFormatter();
        ms.Position = 0;
        return bf1.Deserialize(ms);
    }
    [Serializable]
    class Student
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public int[] LessonIds { get; set; }
        public string[] LessonNames { get; set; }
        public int Id { get; set; }
    }
    static void Main()
    {
        Student obj = new Student();
        obj.UserName = "Admin";
        obj.Password = "Password";
        obj.LessonIds = new int[] { 1, 2, 3, 4, 5 };
        obj.LessonNames = new string[] { "Spanish", "Maths" };
        obj.Id = 43;
        byte[] retByteArray = Crypto.Serialize(obj);

        Student objNew = (Student)Crypto.Deserialize(retByteArray);
    }
}