C#序列化和反序列化
2021-03-09 04:28
标签:ror oid zab == byte rri for pre binary 序列化和反序列化代码如下 测试代码如下 测试序列化基类Request,需要加[Serializable]标签 测试序列化类TestRequest,也需要加[Serializable]标签 C#序列化和反序列化 标签:ror oid zab == byte rri for pre binary 原文地址:https://www.cnblogs.com/xiaobao2017/p/12760762.html ///
void Start()
{
TestRequest request = new TestRequest(100,1, "测试", true);
byte[] bytes = ObjectToBytes(request);
Debug.LogError(bytes.Length);
TestRequest testRequest = BytesToObject(bytes) as TestRequest;
if (testRequest == null)
{
Debug.LogError("反序列化对象为空");
return;
}
Debug.LogError(testRequest.ToString());
}
using System;
[Serializable]
public class Request
{
public int msgType;
public string b;
public bool c;
public Request()
{
}
public Request(int msgType, string b,bool c)
{
this.msgType = msgType;
this.b = b;
this.c = c;
}
public override string ToString()
{
return "Request[msgType:" + msgType + "b:" + b + " c:" + c + "]";
}
}
using System;
[Serializable]
public class TestRequest : Request
{
public int testNum;
public TestRequest(int testNum, int msgType,string b,bool c):base(msgType,b,c)
{
this.testNum = testNum;
}
public override string ToString()
{
return "TestRequest[TestNum:"+testNum+" msgType:" + msgType + " b:" + b + " c:" + c + "]";
}
}