System.Drawing.Font font1 = new Font("宋体",9);
Byte[] bytes = HFSoft.Serialization.Serializable.SerializeObject(font1);
System.Drawing.Font font2 = HFSoft.Serialization.Serializable.DeserializeObject(bytes) as System.Drawing.Font;
MessageBox.Show(this,font2.Name);
这是我做的,没有问题。
Byte[] bytes = HFSoft.Serialization.Serializable.SerializeObject(font1);
System.Drawing.Font font2 = HFSoft.Serialization.Serializable.DeserializeObject(bytes) as System.Drawing.Font;
MessageBox.Show(this,font2.Name);
这是我做的,没有问题。
解决方案 »
- 在线等,,请教大家一个问题,,急急急
- 怎么过通过句柄获得进程名称?
- 求一类似帮助CHM文档控件
- 请问WebBrowser显示中文的问题,谢谢
- POS小票打印(从数据库读取)
- 紧急求助!C#中FileStream如何保存?
- 如何保存datagrid控件和listview控件中的数据到文本文件(崩溃了,请提供源码)
- 急问:winform 中怎么显示数据库表中的记录?用DataGrid吗?
- Datatable合并行,列值累加
- 关于Linq to SQL的多表查询
- 在WEB窗体页面中的DataGrid的处理问题
- ------------------.net试题----------------------
/// 把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public static byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
public static void SerializeObject(object pObj,string pFileName)
{
System.IO.FileStream stream = System.IO.File.Open(pFileName,System.IO.FileMode.Create,System.IO.FileAccess.Write);
byte[] bytes = SerializeObject(pObj);
stream.Write(bytes,0,bytes.Length);
stream.Flush();
stream.Close();
}
public static object DeserializeObject(string pFileName)
{
System.IO.FileStream stream = System.IO.File.Open(pFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes,0,bytes.Length);
stream.Close();
return DeserializeObject(bytes);
}
/// <summary>
/// 把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public static object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if(pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
以上是那几个方法
你的序列化类是C#提供的吗?如果不是,是否可以提供代码呢? 谢谢
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace HFSoft.Serialization
{
/// <summary>
/// 对象序列化对象类
/// </summary>
public class Serializable
{
private Serializable()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public static byte[] SerializeObject(object pObj)
{
if(pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory,pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read,0,read.Length);
_memory.Close();
return read;
}
public static void SerializeObject(object pObj,string pFileName)
{
System.IO.FileStream stream = System.IO.File.Open(pFileName,System.IO.FileMode.Create,System.IO.FileAccess.Write);
byte[] bytes = SerializeObject(pObj);
stream.Write(bytes,0,bytes.Length);
stream.Flush();
stream.Close();
}
public static object DeserializeObject(string pFileName)
{
System.IO.FileStream stream = System.IO.File.Open(pFileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes,0,bytes.Length);
stream.Close();
return DeserializeObject(bytes);
}
/// <summary>
/// 把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public static object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if(pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
}
}