如何反序列化一个被序列化的文件?

zjh222 2009-10-04 01:49:30
在应用.
一.A程序员二进制序列化一个类,保存文件为XX.bin
二.复制XX.bin文件给B程序员.
三.B程序员反序列化这个XX.bin文件.供B调用,怎么做?


...全文
137 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
优效soft 2009-10-07
  • 打赏
  • 举报
回复
要注意的是程序集要以样。
aimeast 2009-10-04
  • 打赏
  • 举报
回复
只要typeof(class)是一样的就可以反序列化
zjh222 2009-10-04
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zanfeng 的回复:]
public static void fnSerializToFile(object v_object, string v_path)//将对象转换为文件
        {
            try
            {
                using (FileStream __fs = System.IO.File.Create(v_path))
                {
                    BinaryFormatter __bf = new BinaryFormatter();
                    __bf.Serialize(__fs, v_object);
                    __fs.Close();
                }1
            }
            catch (Exception ex)
            {
                My.Msg.ExceptionInfo.ShowExceptionError("fnSerializ", ex);
            }
        }

        public static object fnDeSerializFromFile(string v_path)
        {
            try
            {
                using (FileStream __fs = System.IO.File.OpenRead(v_path))
                {

                    byte[] byts = new byte[__fs.Length];
                    __fs.Read(byts, 0, (int)__fs.Length);
                    BinaryFormatter __bf = new BinaryFormatter();
                    object result = __bf.Deserialize(__fs);
                    __fs.Close();
                    return result;
                }
            }
            catch (Exception ex)
            {
                My.Msg.ExceptionInfo.ShowExceptionError("fnDeSerializ", ex);
                return null;
            }

        }//从文件转换为对象

[/Quote]
如果反序列化使用这个类呀/?
ljb07976513524 2009-10-04
  • 打赏
  • 举报
回复
什么就序列化和反序列化?
大侠解释一下
足球中国 2009-10-04
  • 打赏
  • 举报
回复
public static void fnSerializToFile(object v_object, string v_path)//将对象转换为文件
{
try
{
using (FileStream __fs = System.IO.File.Create(v_path))
{
BinaryFormatter __bf = new BinaryFormatter();
__bf.Serialize(__fs, v_object);
__fs.Close();
}
}
catch (Exception ex)
{
My.Msg.ExceptionInfo.ShowExceptionError("fnSerializ", ex);
}
}

public static object fnDeSerializFromFile(string v_path)
{
try
{
using (FileStream __fs = System.IO.File.OpenRead(v_path))
{

byte[] byts = new byte[__fs.Length];
__fs.Read(byts, 0, (int)__fs.Length);
BinaryFormatter __bf = new BinaryFormatter();
object result = __bf.Deserialize(__fs);
__fs.Close();
return result;
}
}
catch (Exception ex)
{
My.Msg.ExceptionInfo.ShowExceptionError("fnDeSerializ", ex);
return null;
}

}//从文件转换为对象

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧