[原创]对象以byte形式序列化与反序列化

[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);

        }

原文地址:https://www.cnblogs.com/xinyuxin912/p/1322326.html