c# 類的序列化,以及嵌套問題

來源:互聯網
上載者:User

標籤:問題   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<TEST>(data);        Debug.LogError(EST.age);        Debug.LogError(EST.name);        Debug.LogError(EST.tes.mo);        Debug.LogError(EST.tes.sum[3]);    }}public static class SerializeHelper{    /// <summary>    /// 使用UTF8編碼將byte數組轉成字串    /// </summary>    /// <param name="data"></param>    /// <returns></returns>    public static string ConvertToString(byte[] data)    {        return Encoding.UTF8.GetString(data, 0, data.Length);    }    /// <summary>    /// 使用指定字元編碼將byte數組轉成字串    /// </summary>    /// <param name="data"></param>    /// <param name="encoding"></param>    /// <returns></returns>    public static string ConvertToString(byte[] data, Encoding encoding)    {        return encoding.GetString(data, 0, data.Length);    }    /// <summary>    /// 使用UTF8編碼將字串轉成byte數組    /// </summary>    /// <param name="str"></param>    /// <returns></returns>    public static byte[] ConvertToByte(string str)    {        return Encoding.UTF8.GetBytes(str);    }    /// <summary>    /// 使用指定字元編碼將字串轉成byte數組    /// </summary>    /// <param name="str"></param>    /// <param name="encoding"></param>    /// <returns></returns>    public static byte[] ConvertToByte(string str, Encoding encoding)    {        return encoding.GetBytes(str);    }    /// <summary>    /// 將對象序列化為位元據     /// </summary>    /// <param name="obj"></param>    /// <returns></returns>    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;    }    /// <summary>    /// 將對象序列化為XML資料    /// </summary>    /// <param name="obj"></param>    /// <returns></returns>    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;    }    /// <summary>    /// 將位元據還原序列化    /// </summary>    /// <param name="data"></param>    /// <returns></returns>    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;    }    /// <summary>    /// 將位元據還原序列化為指定類型對象    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="data"></param>    /// <returns></returns>    public static T DeserializeWithBinary<T>(byte[] data)    {        return (T)DeserializeWithBinary(data);    }    /// <summary>    /// 將XML資料還原序列化為指定類型對象    /// </summary>    /// <typeparam name="T"></typeparam>    /// <param name="data"></param>    /// <returns></returns>    public static T DeserializeWithXml<T>(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# 類的序列化,以及嵌套問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.