Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 820
David Basarab Avatar asked May 04 '09 20:05

David Basarab


People also ask

Can we serialize interface?

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.

Can we serialize interface C#?

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).

Can interface contain property?

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.

How an object can become a serializable?

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.


1 Answers

You have various bugs in your code, otherwise this would work just fine.

  1. In the constructor for ClassA, your are setting an local variable IB, not the object's IB object.
  2. In 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();
}
like image 104
Erich Mirabal Avatar answered Oct 26 '22 02:10

Erich Mirabal