Beispiel #1
0
 /// <summary>
 /// Deserialize a byte array into an object
 /// </summary>
 /// <param name="array">The byte array</param>
 /// <param name="shift">The shift for the array</param>
 /// <param name="type">The type of the object</param>
 /// <returns>An object of the deserialized byte array</returns>
 public static object Deserialize(byte[] array, ref int shift, Type type)
 {
     if (type.IsPrimitive)
     {
         return(NetworkPrimitiveSerializer.Deserialize(array, ref shift, type));
     }
     if (type == typeof(string))
     {
         return(Convert.ChangeType(NetworkStringSerializer.Deserialize(array, ref shift), type));
     }
     if (type.IsEnum)
     {
         return(NetworkEnumSerializer.Deserialize(array, ref shift, typeof(byte), type));
     }
     return(NetworkClassSerializer.Deserialize(array, ref shift, type));
 }
Beispiel #2
0
        /// <summary>
        /// Serialize an object into a byte array
        /// </summary>
        /// <param name="obj">The object to serialize</param>
        /// <typeparam name="T">The type of the object</typeparam>
        /// <returns>A byte array of the serialized object</returns>
        public static byte[] Serialize <T>(T obj)
        {
            var t = typeof(T);

            if (t.IsPrimitive)
            {
                return(NetworkPrimitiveSerializer.Serialize(obj));
            }
            if (t == typeof(string))
            {
                return(NetworkStringSerializer.Serialize((string)(object)obj));
            }
            if (t.IsEnum)
            {
                return(NetworkEnumSerializer.Serialize(obj, typeof(byte)));
            }
            return(NetworkClassSerializer.Serialize(obj));
        }
Beispiel #3
0
        /// <summary>
        /// Deserialize an object depending on its type
        /// </summary>
        /// <param name="type">The type of the object to deserialize</param>
        /// <param name="array">The byte array representing the object</param>
        /// <param name="shift">The shift to apply to the byte array</param>
        /// <param name="decodeSubType">Optional parameter; Used for ListSerializer</param>
        /// <returns>The object deserialized</returns>
        protected static object TypeDeserialize(Type type, byte[] array, ref int shift, bool decodeSubType = false)
        {
            object value;

//            var args = new object[] { array, shift };
            if (type.IsPrimitive)
            {
//                value = InvokeDeserialize(typeof(NetworkPrimitiveSerializer), type, args);
//                shift = (int)args[1];
                value = NetworkPrimitiveSerializer.Deserialize(array, ref shift, type);
            }
            else if (type == typeof(string))
            {
                value = NetworkStringSerializer.Deserialize(array, ref shift);
            }
            else if (type.IsEnum)
            {
//                args = new object[] { array, shift, typeof(byte) };
                value = NetworkEnumSerializer.Deserialize(array, ref shift, typeof(byte), type);
//                value = InvokeDeserialize(typeof(NetworkEnumSerializer), type, args);
//                shift = (int)args[1];
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
//                args = new object[] { array, shift, decodeSubType };
//                value = InvokeDeserialize(typeof(NetworkListSerializer), type, args);
//                shift = (int)args[1];
                value = NetworkListSerializer.Deserialize(array, ref shift, type, decodeSubType);
            }
            else
            {
//                value = InvokeDeserialize(typeof(NetworkClassSerializer), type, args);
//                shift = (int)args[1];
                value = NetworkClassSerializer.Deserialize(array, ref shift, type);
            }
            return(value);
        }
Beispiel #4
0
        /// <summary>
        /// Deserialize a byte array into an object
        /// </summary>
        /// <param name="array">The byte array</param>
        /// <param name="shift">The shift for the array</param>
        /// <param name="cast">The type of the enum</param>
        /// <param name="type">The type of the object</param>
        /// <returns>An object of the deserialized byte array</returns>
        public static object Deserialize(byte[] array, ref int shift, Type cast, Type type)
        {
            var val = NetworkPrimitiveSerializer.Deserialize(array, ref shift, cast);

            return(Enum.ToObject(type, val));
        }
Beispiel #5
0
 /// <summary>
 /// Serialize an object into a byte array
 /// </summary>
 /// <param name="obj">The object to serialize</param>
 /// <param name="cast">The type of the enum</param>
 /// <typeparam name="T">The type of the object</typeparam>
 /// <returns>A byte array of the serialized object</returns>
 public static byte[] Serialize <T>(T obj, Type cast)
 {
     return(NetworkPrimitiveSerializer.Serialize(Convert.ChangeType(obj, cast)));
 }