/// <summary>Adds an object to the packet.</summary> /// <param name="_value">The object to add.</param> public void Write <T>(T _value) { byte[] _objectByteArray = ClassSerializer.ObjectToByteArray(_value); //serialize object and convert it to byte array Write(_objectByteArray.Length); // Add the length of the object byte array to the packet Write(_objectByteArray); // Add the object byte array itself }
/// <summary>Reads an object from the packet.</summary> /// <param name="_moveReadPos">Whether or not to move the buffer's read position.</param> public T ReadObject <T>(bool _moveReadPos = true) { try { int _length = ReadInt(_moveReadPos); // Get the length of the object bytes array byte[] _objectByteArray = ReadBytes(_length, _moveReadPos); // Get the object byte array return((T)ClassSerializer.ByteArrayToObject(_objectByteArray)); // Convert the byte array and return the object } catch { throw new Exception("Could not read value of type 'object'!"); } }