Binary serializing an object which composites an interface field without serializing the interface implementor  
Author Message
muysal





PostPosted: .NET Base Class Library, Binary serializing an object which composites an interface field without serializing the interface implementor Top

I want to serialize an object which composites an interface. While serializing the actual class, I do not want to serialize the object that implements this interface. I just want to keep its reference and pass it to the serialized class. Here is an example

[Serializable]

public class Sample

{

private string _name;

private IInterface _sampleImplementation;

public Sample(IInterface interface)

{

_sampleImplementation = interface;

}

}

When I use custom serialization by implementing ISerializable, in the GetObjectData method anything I add to the SerializationInfo gets serialized automatically. I just want to keep the reference of the object that implements the IInterface and pass it to the serialized object when the serialization is over.

Can this be done using built-in binary serialization or should I use a custom framework Does anybody know such a framework

Thanks in advance for your help.

Murat



.NET Development26  
 
 
Indian Ocean





PostPosted: .NET Base Class Library, Binary serializing an object which composites an interface field without serializing the interface implementor Top

Hi,

As per your sample code, Sample class will be serialized as a whole.

AFAIK, I think so that its not possible what you are going to do with built-in mechanizm and i am also wondering that any custom framework can do such thing...

You can remove some of things from your class when you serialize it so those items will be not serialized but if you make your interface object variable serializable then if the object is assigned to it then it will be serialized for sure.

Correct me if I am in wrong direction,

HTH,



 
 
muysal





PostPosted: .NET Base Class Library, Binary serializing an object which composites an interface field without serializing the interface implementor Top

Firstly thanks for your response. I appreciate your time.

In order to do what I want, I had to use custom binary serializer by implementing ISerializable interface in the Sample class. I annotated the interface member as [NonSerialized] and in the GetObjectData method, I kept the reference of interface member in a different data structure within another class and reassigned it after the class is serialized. This is roughly how I got it working but would appreciate more elegant solution if possible.

Murat