对象如何转换为byte[]?
我用下面的代码可以将对象转换为byte[],可是对象数组怎么转换呢?
[code=C#][/code]
Class2 c=new Class2();
c.A="faf";
c.B="ffff";
c.C="测试";
BinaryFormatter bf=new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, c);
byte[] bb = ms.GetBuffer();
比如:Class2[] c=new Class[2];
那么这个c怎么转换成byte[]呢?用上面的方法不行?
对这点不太熟悉.谢谢!
------解决方案--------------------例如:
[Serializable]
public class MyObject : ISerializable
{
public int n1;
public int n2;
public String str;
public MyObject()
{
}
protected MyObject(SerializationInfo info, StreamingContext context)
{
n1 = info.GetInt32("i");
n2 = info.GetInt32("j");
str = info.GetString("k");
}
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("i", n1);
info.AddValue("j", n2);
info.AddValue("k", str);
}
}