internal static void Serialize( Stream destination, ISerializable obj, Dictionary <Type, int> typeToCodeMapping, string formatExceptionMessage) { destination.ThrowIfNull(nameof(destination)); obj.ThrowIfNull(nameof(obj)); typeToCodeMapping.ThrowIfNull(nameof(typeToCodeMapping)); formatExceptionMessage.ThrowIfNull(nameof(formatExceptionMessage)); if (!typeToCodeMapping.TryGetValue(obj.GetType(), out int value)) { throw new FormatException(formatExceptionMessage); } var bytes = BitConverter.GetBytes(value); destination.Write(bytes, 0, bytes.Length); obj.GetSerializer().Serialize(destination); }