public static byte[] Serialize(AuthorIdentity identity) { var identityAsArray = identity.UserId.ToByteArray(); var identityEncoding = Encoding.UTF8.GetBytes(identity.Name); var serializedIdentity = identityAsArray.Concat(identityEncoding).ToArray(); return serializedIdentity; }
public static AuthorIdentity Deserialize(byte[] identityAsArray) { if (identityAsArray.Length < 17) { throw new ArgumentException("AuthIdentity must be at least 17 bytes."); } var tempByteArray = new byte[16]; var arrayWhereToBeCopied = new byte[identityAsArray.Length - 16]; Buffer.BlockCopy(identityAsArray, 0, tempByteArray, 0, 16); Buffer.BlockCopy(identityAsArray, 16, arrayWhereToBeCopied, 0, arrayWhereToBeCopied.Length); var deserializedArrayIdentity = new AuthorIdentity(new Guid(tempByteArray), Encoding.UTF8.GetString(arrayWhereToBeCopied)); return deserializedArrayIdentity; }