I have 2 interfaces IA and IB.
public interface IA
{
IB InterfaceB { get; set; }
}
public interface IB
{
IA InterfaceA { get; set; }
void SetIA(IA value);
}
Each interfaces references the other.
I am trying to serialize ClassA as defined below.
[Serializable]
public class ClassA : IA
{
public IB InterfaceB { get; set; }
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
{
public IA InterfaceA { get; set; }
public void SetIA(IA value)
{
this.InterfaceA = value as ClassA;
}
}
I get an error when I try too serialize because the 2 properties are interfaces. I want to serialize the properties.
How would I get around this?
I need to have references in each interface to the other. And I need to be able to serialize the class back and forth.
The interface must be implemented by any class which uses the interface for serializing its objects. By default, the classes such as wrapper classes and the String class implement the interface java.
When the serializer should serialize that object it knows that the object implements that interface, all it really has to do is serialize it and attach the type attribute (like it does if you serialize abstract classes or just super-classes in general).
Beginning with C# 8.0, an interface may define a default implementation for members, including properties. Defining a default implementation for a property in an interface is rare because interfaces may not define instance data fields.
To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With