Serialization Problem

Good time of day.
For some time I was distracted from Unity3d, in order to write my own Network Suite.
Just a couple of days ago, faced with a problem that I can’t solve :face_with_spiral_eyes:

It consists of the following:

Conventionally, there are three scripts.

Vector2Event.cs

using System;
using System.IO;
using UnityEngine;

namespace TestServerKenderEvents
{
    [Serializable]
    public class Vector2Event : KenderEventSet.Event
    {
        public Vector2Event(Vector2 vector2)
        {
            this.x = vector2.x;
            this.y = vector2.y;

            this.Vector2 = vector2;
        }

        public Vector2Event(BinaryReader reader)
        {
            this.x = reader.ReadSingle();
            this.y = reader.ReadSingle();

            this.Vector2 = new Vector2(this.x, this.y);
        }

        public Vector2 Vector2
        {
            get;
            private set;
        }

        public float x
        {
            get;
            private set;
        }

        public float y
        {
            get;
            private set;
        }

        public override void Serialize(BinaryWriter writer)
        {
            writer.Write(this.x);
            writer.Write(this.y);
        }
    }
}

JoinRequest.cs

using System;
using System.IO;
using System.Collections;
using UnityEngine;

namespace TestServerKenderEvents
{
    [Serializable]
    public class JoinRequest : KenderEventSet.Event
    {
        public JoinRequest(System.Drawing.Color color, Vector2 vector2, float radiusOfInterest)
        {
            this.Color = color;
            this.Vector2 = vector2;
            this.RadiusOfInterest = radiusOfInterest;
        }

        public JoinRequest(BinaryReader reader)
        {
            this.Color = new ColorEvent(reader).Color;
            this.Vector2 = new Vector2Event(reader).Vector2;
            this.RadiusOfInterest = reader.ReadSingle();
        }

        public System.Drawing.Color Color
        {
            get;
            private set;
        }

        public Vector2 Vector2
        {
            get;
            private set;
        }

        public float RadiusOfInterest
        {
            get;
            private set;
        }

        public override void Serialize(BinaryWriter writer)
        {
            new ColorEvent(this.Color).Serialize(writer);
            new Vector2Event(this.Vector2).Serialize(writer);
            writer.Write(this.RadiusOfInterest);
        }

    }
}

KenderEventSet.cs

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace KenderEventSet
{
    public static class EventSet
    {
        public static object Deserialize(byte[] message)
        {
            BinaryFormatter bin = new BinaryFormatter();
            MemoryStream mem = new MemoryStream();
            mem.Write(message, 0, message.Length);
            mem.Seek(0, 0);
            return bin.Deserialize(mem);
        }

        public static byte[] Serialize(object Object)
        {
            try
            {
                BinaryFormatter bin = new BinaryFormatter();
                MemoryStream mem = new MemoryStream();
                bin.Serialize(mem, Object);                            // KenderEventSet.cs:string 24
                return mem.GetBuffer();
            }
            catch (SerializationException)
            {
                return null;
            }
        }
    }

    [Serializable]
    public abstract class Event
    {
        public abstract void Serialize(BinaryWriter writer);
    }
}

… And when I call the following code:

...

playerEventInterlocutor.SendEvent(                                   // Form1.cs:string 409
    TestServerKenderEventSet.Serialize(
        new JoinRequest(
            this.player1Pen.Color,
            new Vector2(
                this.player1XPosition,
                this.player1YPosition
            ),
            this.areaOfInterest
        )
    ),
    false
);

...

…, I get the funny exception:

Simply put, I think swearing on the non-serializable Vector2 unfounded, because I write only its coordinates.
Write a surrogate for every such case is no desire, for it may eventually make the library’s users crazy :rage:

In general, I would be grateful to anyone who can help! Thanks in advance!

Well, I used GeneralSerializableAdapter from here, so i got the following code:

KenderEventSet.cs

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace KenderEventSet
{
    [Serializable]
    public class GeneralSerializableAdapter : ISerializable
    {
        [NonSerialized]
        private object obj;
        public GeneralSerializableAdapter(Object o)
        {
            obj = o;
        }
        private GeneralSerializableAdapter(SerializationInfo info, StreamingContext context)
        {
            obj = Assembly.GetExecutingAssembly().CreateInstance(info.GetString("class"));
            PropertyInfo[] allp = obj.GetType().GetProperties();
            foreach (PropertyInfo p in allp)
            {
                if (p.PropertyType == typeof(Int32))
                {
                    p.SetValue(obj, info.GetInt32(p.Name), null);
                }
                else if (p.PropertyType == typeof(Double))
                {
                    p.SetValue(obj, info.GetDouble(p.Name), null);
                }
                else if (p.PropertyType == typeof(String))
                {
                    p.SetValue(obj, info.GetString(p.Name), null);
                }
                else if (p.PropertyType == typeof(Boolean))
                {
                    p.SetValue(obj, info.GetBoolean(p.Name), null);
                }
                else if (p.PropertyType == typeof(Byte))
                {
                    p.SetValue(obj, info.GetByte(p.Name), null);
                }
                else if (p.PropertyType == typeof(Char))
                {
                    p.SetValue(obj, info.GetChar(p.Name), null);
                }
                else if (p.PropertyType == typeof(DateTime))
                {
                    p.SetValue(obj, info.GetDateTime(p.Name), null);
                }
                else if (p.PropertyType == typeof(Decimal))
                {
                    p.SetValue(obj, info.GetDecimal(p.Name), null);
                }
                else if (p.PropertyType == typeof(Int16))
                {
                    p.SetValue(obj, info.GetInt16(p.Name), null);
                }
                else if (p.PropertyType == typeof(Int64))
                {
                    p.SetValue(obj, info.GetInt64(p.Name), null);
                }
                else if (p.PropertyType == typeof(SByte))
                {
                    p.SetValue(obj, info.GetSByte(p.Name), null);
                }
                else if (p.PropertyType == typeof(Single))
                {
                    p.SetValue(obj, info.GetSingle(p.Name), null);
                }
                else if (p.PropertyType == typeof(UInt16))
                {
                    p.SetValue(obj, info.GetUInt16(p.Name), null);
                }
                else if (p.PropertyType == typeof(UInt32))
                {
                    p.SetValue(obj, info.GetUInt32(p.Name), null);
                }
                else if (p.PropertyType == typeof(UInt64))
                {
                    p.SetValue(obj, info.GetUInt64(p.Name), null);
                }
            }
        }
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (info == null)
                throw new System.ArgumentNullException("info");
            info.AddValue("class", obj.GetType().FullName);
            PropertyInfo[] allp = obj.GetType().GetProperties();
            foreach (PropertyInfo p in allp)
            {
                info.AddValue(p.Name, p.GetValue(obj, null));
            }
        }
        public object Obj
        {
            get
            {
                return obj;
            }
        }
    }

    public static class EventSet
    {
        public static byte[] Serialize(Object o)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, new GeneralSerializableAdapter(o));
            return ms.ToArray();
        }
        public static object Deserialize(byte[] b)
        {
            MemoryStream ms = new MemoryStream(b);
            BinaryFormatter bf = new BinaryFormatter();
            ms.Position = 0;
            return ((GeneralSerializableAdapter)bf.Deserialize(ms)).Obj;
        }
    }

    [Serializable]
    public abstract class Event
    {
        public abstract void Serialize(BinaryWriter writer);
    }
}

But when I ran this code, I got the same message:

… despite the fact that I still do not serialize this damn Vector2.