二进制序列化反序列化

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class xmlxuleihua : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

        /*   TestSerial t1 = XmlDeSerlize();
           Debug.Log(t1.Id);*/
        BinarySerTest();
    }
    //开始序列化 
   /* void XmlSerilize(TestSerial testSerial)
    {   
        FileStream fileStream =new FileStream(Application.dataPath+"/test.xml",FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);//打开文件流
        StreamWriter writer = new StreamWriter(fileStream,System.Text.Encoding.UTF8);//写入流
        XmlSerializer serializer = new XmlSerializer(testSerial.GetType());//获取类型
        serializer.Serialize(writer, testSerial);
        writer.Close();
        fileStream.Close();
    }
    TestSerial XmlDeSerlize()
    {
        FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        XmlSerializer xs = new XmlSerializer(typeof(TestSerial));
        TestSerial testSerial =(TestSerial) xs.Deserialize(fs);
        fs.Close();
        return testSerial;
    }*/
    
    void BinarySerTest()
    {
        TestSerial testSerial = new TestSerial();
        testSerial.Name = "1";
        testSerial.Id = 1;
        BinarySerlize(testSerial);
        TestSerial testSerial1 = BinaryDeserlize();
        print(testSerial1.Id);

    }
    void BinarySerlize(TestSerial testSerial)
    {
        FileStream fs = new FileStream(Application.dataPath + "/test.bytes", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        BinaryFormatter bf=new BinaryFormatter();
        bf.Serialize(fs, testSerial);
        fs.Close();
    }
    TestSerial BinaryDeserlize()
    {
        TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/test.bytes");
        MemoryStream stream = new MemoryStream(textAsset.bytes);
        BinaryFormatter bf = new BinaryFormatter();
    
        TestSerial testSerial= (TestSerial)bf.Deserialize(stream);
        stream.Close();
        return testSerial;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中的二进制序列化反序列化是一种将对象转换为二进制数据流以便存储或传输,并且可以将二进制数据流还原为对象的过程。这种序列化方式可以用于在不同平台或不同应用程序之间传递对象数据。 在C#中,可以使用BinaryFormatter类来进行二进制序列化反序列化操作。下面是一个简单的示例: ```csharp using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public class Person { public string Name { get; set; } public int Age { get; set; } } public class Program { public static void Main() { // 创建一个Person对象 Person person = new Person { Name = "Alice", Age = 25 }; // 将对象序列化二进制数据流 BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, person); // 将二进制数据流反序列化为对象 stream.Position = 0; Person deserializedPerson = (Person)formatter.Deserialize(stream); // 输出反序列化后的对象属性 Console.WriteLine($"Name: {deserializedPerson.Name}, Age: {deserializedPerson.Age}"); } } ``` 在上面的示例中,我们定义了一个名为Person的类,并使用[Serializable]特性标记该类可以进行序列化。然后,我们创建了一个Person对象,并使用BinaryFormatter类将其序列化二进制数据流。接着,我们将二进制数据流反序列化为一个新的Person对象,并输出其属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值