I need to serialize a class C using BinaryFormatter.
So I mark C as [Serializable].
C inherits an abstract B class.
B is NOT marked as [Serializable].
B inherits A class,
A is [Serializable] and also it implements ISerializable;
What situation can we have?
AProperty will be serialized
BProperty will not be serialized (?)
CProperty will be serialized
Your edit changes everything; if
AimplementsISerializablethen they all implementISerializable. With that, you are using custom serialization so the behaviour is determined entirely by theGetObjectDataand serialization-constructor code you write in each ofA/B/C.Original answer, when there was no mention of
ISerializableserialization is generally based on type of the actual object at runtime, and
BinaryFormatter(which maps to[Serializable]) is a field serializer. However, ifBis not marked[Serializable], it cannot be serialized withBinaryFormatter(unless it isISerializable); so:A, allAfields will be serializedC, it will throw an exceptionBsince that is abstract; but if it was, that would throw too)If you aren’t using
BinaryFormatter(and personally I don’t thinkBinaryFormatteris a good choice most of the time), then the behaviour will depend on the specific serializer.My advice: use a different serializer; there are plenty. Almost all are (IMO) better choices than
BinaryFormatter.