Exemplo n.º 1
0
        public static byte[] Serialize(object obj)
        {
            if (obj == null)
            {
                return(new byte[0]);
            }
            Type type = obj.GetType();

            if (type.IsEnum)
            {
                return(BloxSerializer.GetBytes((int)obj));
            }
            if (type.IsArray)
            {
                return(BloxSerializer.SerializeArray(obj));
            }
            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(List <>))
                {
                    return(BloxSerializer.SerializeList(obj));
                }
                return(new byte[0]);
            }
            if (BloxSerializer.writers.ContainsKey(type))
            {
                return(BloxSerializer.writers[type](obj));
            }
            return(new byte[0]);
        }
Exemplo n.º 2
0
 static BloxSerializer()
 {
     BloxSerializer.writers = new Dictionary <Type, Func <object, byte[]> >();
     BloxSerializer.readers = new Dictionary <Type, Func <byte[], object> >();
     BloxSerializer.writers[typeof(bool)]       = ((object value) => BloxSerializer.GetBytes((bool)value));
     BloxSerializer.writers[typeof(byte)]       = ((object value) => BloxSerializer.GetBytes((byte)value));
     BloxSerializer.writers[typeof(sbyte)]      = ((object value) => BloxSerializer.GetBytes((sbyte)value));
     BloxSerializer.writers[typeof(char)]       = ((object value) => BloxSerializer.GetBytes((char)value));
     BloxSerializer.writers[typeof(int)]        = ((object value) => BloxSerializer.GetBytes((int)value));
     BloxSerializer.writers[typeof(uint)]       = ((object value) => BloxSerializer.GetBytes((uint)value));
     BloxSerializer.writers[typeof(short)]      = ((object value) => BloxSerializer.GetBytes((short)value));
     BloxSerializer.writers[typeof(ushort)]     = ((object value) => BloxSerializer.GetBytes((ushort)value));
     BloxSerializer.writers[typeof(long)]       = ((object value) => BloxSerializer.GetBytes((long)value));
     BloxSerializer.writers[typeof(ulong)]      = ((object value) => BloxSerializer.GetBytes((ulong)value));
     BloxSerializer.writers[typeof(float)]      = ((object value) => BloxSerializer.GetBytes((float)value));
     BloxSerializer.writers[typeof(double)]     = ((object value) => BloxSerializer.GetBytes((double)value));
     BloxSerializer.writers[typeof(decimal)]    = ((object value) => BloxSerializer.GetBytes((decimal)value));
     BloxSerializer.writers[typeof(string)]     = ((object value) => BloxSerializer.GetBytes((string)value));
     BloxSerializer.writers[typeof(Vector2)]    = ((object value) => BloxSerializer.GetBytes((Vector2)value));
     BloxSerializer.writers[typeof(Vector3)]    = ((object value) => BloxSerializer.GetBytes((Vector3)value));
     BloxSerializer.writers[typeof(Vector4)]    = ((object value) => BloxSerializer.GetBytes((Vector4)value));
     BloxSerializer.writers[typeof(Quaternion)] = ((object value) => BloxSerializer.GetBytes((Quaternion)value));
     BloxSerializer.writers[typeof(Rect)]       = ((object value) => BloxSerializer.GetBytes((Rect)value));
     BloxSerializer.writers[typeof(Color)]      = ((object value) => BloxSerializer.GetBytes((Color)value));
     BloxSerializer.writers[typeof(Color32)]    = ((object value) => BloxSerializer.GetBytes((Color32)value));
     BloxSerializer.readers[typeof(bool)]       = ((byte[] data) => BloxSerializer.ToBool(data));
     BloxSerializer.readers[typeof(byte)]       = ((byte[] data) => BloxSerializer.ToByte(data));
     BloxSerializer.readers[typeof(sbyte)]      = ((byte[] data) => BloxSerializer.ToSByte(data));
     BloxSerializer.readers[typeof(char)]       = ((byte[] data) => BloxSerializer.ToChar(data));
     BloxSerializer.readers[typeof(int)]        = ((byte[] data) => BloxSerializer.ToInt(data));
     BloxSerializer.readers[typeof(uint)]       = ((byte[] data) => BloxSerializer.ToUInt(data));
     BloxSerializer.readers[typeof(short)]      = ((byte[] data) => BloxSerializer.ToShort(data));
     BloxSerializer.readers[typeof(ushort)]     = ((byte[] data) => BloxSerializer.ToUShort(data));
     BloxSerializer.readers[typeof(long)]       = ((byte[] data) => BloxSerializer.ToLong(data));
     BloxSerializer.readers[typeof(ulong)]      = ((byte[] data) => BloxSerializer.ToULong(data));
     BloxSerializer.readers[typeof(float)]      = ((byte[] data) => BloxSerializer.ToFloat(data));
     BloxSerializer.readers[typeof(double)]     = ((byte[] data) => BloxSerializer.ToDouble(data));
     BloxSerializer.readers[typeof(decimal)]    = ((byte[] data) => BloxSerializer.ToDecimal(data));
     BloxSerializer.readers[typeof(string)]     = ((byte[] data) => BloxSerializer.ToString(data));
     BloxSerializer.readers[typeof(Vector2)]    = ((byte[] data) => BloxSerializer.ToVector2(data));
     BloxSerializer.readers[typeof(Vector3)]    = ((byte[] data) => BloxSerializer.ToVector3(data));
     BloxSerializer.readers[typeof(Vector4)]    = ((byte[] data) => BloxSerializer.ToVector4(data));
     BloxSerializer.readers[typeof(Quaternion)] = ((byte[] data) => BloxSerializer.ToQuaternion(data));
     BloxSerializer.readers[typeof(Rect)]       = ((byte[] data) => BloxSerializer.ToRect(data));
     BloxSerializer.readers[typeof(Color)]      = ((byte[] data) => BloxSerializer.ToColor(data));
     BloxSerializer.readers[typeof(Color32)]    = ((byte[] data) => BloxSerializer.ToColor32(data));
 }