/// <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)); }
/// <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); }
/// <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)); }