I want to serailize my object and used BinaryFormatter
class.
public static byte[] BinarySerialize(IMessage message)
{
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, message);
return stream.ToArray();
}
}
But when I run the code, throws an exception.
SerializationException: Object is not marked as serializable.
I think this exception thrown by BinaryFormatter.
I do not want to mark as [Serializable]
my objects. Or my library users may forget mark as [Serializable]
their own Messages.
Is there any other way to binary serialize my objects without using [Serializable] attribute?