How can I serialize an object that has an interface as a property?

后端 未结 5 1859
天涯浪人
天涯浪人 2021-01-05 01:45

I have 2 interfaces IA and IB.

public interface IA
{
    IB InterfaceB { get; set;  }
}

public interface IB
{
    IA InterfaceA { get; set;  }

    void Set         


        
5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 02:24

    In return for your question: two other questions. What do you Serialize? Is the database reference-aware?

    You don't serialize interfaces; you serialize objects. The object you serialize is either an implementation of IA or IB.

    It's up to the serialized object to decide whether one of it's properties should be serialized or not. If the property does need serializing, it should implement the Serializable interface.

    You can only serialize a so called 'island' formed by a circular reference A <-> B if the database can identify the serialized objects: it should first 'allocate' space for A, start serializing A's properties. When it arrives at B, it will find one of it's properties referring to A. The serialization should then include a reference to the serialized version of A.

    It's much like two acquaintances moving houses at the same time: first they will exchange their future adresses, only then they will physically move.

提交回复
热议问题