c# 类的序列化,以及嵌套问题

2021-04-30 18:27

阅读:318

标签:问题   name   coding   字符编码   call   class   utf8   string   returns   

简单的序列化,网上很多,但是突然想到一个问题,如果一个类里用到了另一个,那么怎么办,今天试了试,只需要加上序列号标签就可以了

 

 

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

[Serializable]
public class TEST
{
    public string name;
    public int age;
    public Test01 tes;
}

[Serializable]
public class Test01
{
    public float mo;
    public int[] sum;
}

public class Seralizabletest : MonoBehaviour {
    public byte[] data;
    // Use this for initialization
    void Start () {
        Test();
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            DeTest();
        }
    }

    void Test()
    {
        TEST EST = new TEST();
        EST.age = 10;
        EST.name = "lizhiyong";
        Test01 test01 = new Test01();
        test01.mo = 0.5f;
        test01.sum = new int[4] { 0, 1, 2, 3 };
        EST.tes = test01;

        data = SerializeHelper.SerializeToBinary(EST);
    }

    void DeTest()
    {
        TEST EST = SerializeHelper.DeserializeWithBinary(data);
        Debug.LogError(EST.age);
        Debug.LogError(EST.name);
        Debug.LogError(EST.tes.mo);
        Debug.LogError(EST.tes.sum[3]);
    }


}

public static class SerializeHelper
{
    /// 
    /// 使用UTF8编码将byte数组转成字符串
    /// 
    /// 
    /// 
    public static string ConvertToString(byte[] data)
    {
        return Encoding.UTF8.GetString(data, 0, data.Length);
    }

    /// 
    /// 使用指定字符编码将byte数组转成字符串
    /// 
    /// 
    /// 
    /// 
    public static string ConvertToString(byte[] data, Encoding encoding)
    {
        return encoding.GetString(data, 0, data.Length);
    }

    /// 
    /// 使用UTF8编码将字符串转成byte数组
    /// 
    /// 
    /// 
    public static byte[] ConvertToByte(string str)
    {
        return Encoding.UTF8.GetBytes(str);
    }

    /// 
    /// 使用指定字符编码将字符串转成byte数组
    /// 
    /// 
    /// 
    /// 
    public static byte[] ConvertToByte(string str, Encoding encoding)
    {
        return encoding.GetBytes(str);
    }

    /// 
    /// 将对象序列化为二进制数据 
    /// 
    /// 
    /// 
    public static byte[] SerializeToBinary(object obj)
    {
        MemoryStream stream = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(stream, obj);

        byte[] data = stream.ToArray();
        stream.Close();

        return data;
    }

    /// 
    /// 将对象序列化为XML数据
    /// 
    /// 
    /// 
    public static byte[] SerializeToXml(object obj)
    {
        MemoryStream stream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(obj.GetType());
        xs.Serialize(stream, obj);

        byte[] data = stream.ToArray();
        stream.Close();

        return data;
    }

    /// 
    /// 将二进制数据反序列化
    /// 
    /// 
    /// 
    public static object DeserializeWithBinary(byte[] data)
    {
        MemoryStream stream = new MemoryStream();
        stream.Write(data, 0, data.Length);
        stream.Position = 0;
        BinaryFormatter bf = new BinaryFormatter();
        object obj = bf.Deserialize(stream);

        stream.Close();

        return obj;
    }

    /// 
    /// 将二进制数据反序列化为指定类型对象
    /// 
    /// 
    /// 
    /// 
    public static T DeserializeWithBinary(byte[] data)
    {
        return (T)DeserializeWithBinary(data);
    }

    /// 
    /// 将XML数据反序列化为指定类型对象
    /// 
    /// 
    /// 
    /// 
    public static T DeserializeWithXml(byte[] data)
    {
        MemoryStream stream = new MemoryStream();
        stream.Write(data, 0, data.Length);
        stream.Position = 0;
        XmlSerializer xs = new XmlSerializer(typeof(T));
        object obj = xs.Deserialize(stream);

        stream.Close();

        return (T)obj;
    }
}

 

c# 类的序列化,以及嵌套问题

标签:问题   name   coding   字符编码   call   class   utf8   string   returns   

原文地址:http://www.cnblogs.com/lzy575566/p/7803298.html


评论


亲,登录后才可以留言!