[Serializable]
private class TestClass
{
public string TestName;
public string TestValue;
}
private void ttt()
{
TestClass tt = new TestClass();
tt.TestValue = "Hello Value!";
tt.TestName = "Hello!";
BinaryFormatter formater = new BinaryFormatter();
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
formater.Serialize(memStream,tt);
byte[] bytes;
bytes = memStream.GetBuffer();
//传递数据
//获取到byte[]
BinaryFormatter RFormater = new BinaryFormatter();
System.IO.MemoryStream RmemStream = new System.IO.MemoryStream();
RmemStream.Write(bytes, 0, bytes.Length);
RmemStream.Position = 0;
TestClass t2 = RFormater.Deserialize(RmemStream) as TestClass;
MessageBox.Show(t2.TestName);
}