Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using Omanju.Byteproc;
- using Omanju.Byteproc.Processors;
- public class BaseClass {
- public int num;
- public BaseClass(int num) {
- this.num = num;
- }
- public override string ToString() {
- return string.Format("num = {0}", num);
- }
- public static readonly IValueProcessor<BaseClass> processor = ClassConverterBuilder.Create(() => new BaseClass(0))
- .AddInt(o => o.num, (o,v) => o.num = v)
- .ToProcessor();
- public static readonly IConverter<BaseClass> converter = new Converter<BaseClass>(
- new PolymorphicProcessor<BaseClass>(ByteOrderType.LittleEndian)
- .Add(0, processor)
- .Add(1, SubClass.processor)
- );
- }
- public class SubClass : BaseClass {
- public string str;
- public SubClass(int num, string str) : base(num) {
- this.str = str;
- }
- public override string ToString() {
- return string.Format("num = {0}, str = {1}", num, str);
- }
- new public static readonly IValueProcessor<SubClass> processor = ClassConverterBuilder.Create(() => new SubClass(0, null))
- .AddInt(o => o.num, (o,v) => o.num = v)
- .AddString(o => o.str, (o,v) => o.str = v)
- .ToProcessor();
- }
- public class Sample03 : MonoBehaviour {
- void Start () {
- BaseClass b = new BaseClass(123);
- byte[] bytes_base = BaseClass.converter.Serialize(b);
- BaseClass s1 = new SubClass(456, "ABC");
- byte[] bytes_sub = BaseClass.converter.Serialize(s1);
- BaseClass res1 = BaseClass.converter.Deserialize(bytes_base);
- BaseClass res2 = BaseClass.converter.Deserialize(bytes_sub);
- Debug.LogFormat("1: type = {0}, {1}", res1.GetType().Name, res1.ToString());
- Debug.LogFormat("2: type = {0}, {1}", res2.GetType().Name, res2.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement