I have 2 interfaces IA and IB.
public interface IA
{
IB InterfaceB { get; set; }
}
public interface IB
{
IA InterfaceA { get; set; }
void Set
You have various bugs in your code, otherwise this would work just fine.
ClassA
, your are setting an local variable IB, not the object's IB object.ClassB
, you are casting back to the object concrete class, instead of leaving it alone as the interface type.Here is what your code should look like:
public interface IA
{
IB InterfaceB { get; set; }
}
public interface IB
{
IA InterfaceA { get; set; }
void SetIA(IA value);
}
[Serializable]
public class ClassA : IA
{
public IB InterfaceB { get; set; }
public ClassA()
{
// Call outside function to get Interface B
this.InterfaceB = new ClassB();
// Set IB to have A
InterfaceB.SetIA(this);
}
}
[Serializable]
public class ClassB : IB
{
public IA InterfaceA { get; set; }
public void SetIA(IA value)
{
this.InterfaceA = value;
}
}
[STAThread]
static void Main()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bin = new BinaryFormatter();
ClassA myA = new ClassA();
bin.Serialize(ms, myA);
ms.Position = 0;
ClassA myOtherA = bin.Deserialize(ms) as ClassA;
Console.ReadLine();
}
Implement ISerializable on your objects to control the serialization.
[Serializable]
public class ClassB : IB, ISerializable
{
public IA InterfaceA { get; set; }
public void SetIA(IA value)
{
this.InterfaceA = value as ClassA;
}
private MyStringData(SerializationInfo si, StreamingContext ctx) {
Type interfaceAType = System.Type.GetType(si.GetString("InterfaceAType"));
this.InterfaceA = si.GetValue("InterfaceA", interfaceAType);
}
void GetObjectData(SerializationInfo info, StreamingContext ctx) {
info.AddValue("InterfaceAType", this.InterfaceA.GetType().FullName);
info.AddValue("InterfaceA", this.InterfaceA);
}
}
Interface properties are not serializable. However, fields that reference those properties (in the subclass) are.
You'll need to do something like this:
[Serializable]
public class ClassA : IA
{
private IB _interfaceB;
public IB InterfaceB { get { return _interfaceB; } set { _interfaceB = value; } }
public ClassA()
{
// Call outside function to get Interface B
IB interfaceB = Program.GetInsanceForIB();
// Set IB to have A
interfaceB.SetIA(this);
}
}
[Serializable]
public class ClassB : IB
{
private IA _interfaceA;
public IA InterfaceA { get { return _interfaceA; } set { _interfaceA = value; } }
public void SetIA(IA value)
{
this.InterfaceA = value as ClassA;
}
}
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.
Assuming you don't want to serialize the interface property, place the following attribute
[NonSerialized]
on the interface property.