Reputation: 24676
I need to serialize complex objects in my project and put them in a database. I'd like to serialize them using XML for obtain a easier debugging of my application.
My case is very similar to what is described in this article: http://geekswithblogs.net/SoftwareDoneRight/archive/2008/01/16/how-to-serialize-an-interface-using-the-xmlserializer.aspx
So I have an object containing a Property which type is defined by an interface. Then I have different concrete types implementing it.
Following the article approach, using the XmlInclude
attribute, I obtain a strong coupled solution, but my application is structured to use a plug-in approach, so I could have so many implementations as I want of my interface.
Is there a way to solve my issue using xml serialization, or I have to go with binary serialization?
Upvotes: 1
Views: 4427
Reputation: 9680
Read your post from geekswithblogs.net. I will suggest you to do the binary serialization. It is easy to implement and to maintain (unless and until you change the qualified name of the class). Binary Serializer serializes private members too.
How to use binary serializer
/// <summary>
/// Serializes the given object to byte stream
/// </summary>
public sealed class Serializer {
/// <summary>
/// Serializes the given object to byte stream
/// </summary>
/// <param name="objectToSeralize">Object to be serialized</param>
/// <returns>byte array of searialize object</returns>
public static byte[] Serialize(object objectToSeralize) {
byte[] objectBytes;
using (MemoryStream stream = new MemoryStream()) {
//Creating binary formatter to serialize object.
BinaryFormatter formatter = new BinaryFormatter();
//Serializing objectToSeralize.
formatter.Serialize(stream, objectToSeralize);
objectBytes = stream.ToArray();
}
return objectBytes;
}
/// <summary>
/// De-Serialize the byte array to object
/// </summary>
/// <param name="arrayToDeSerialize">Byte array of Serialize object</param>
/// <returns>De-Serialize object</returns>
public static object DeSerialize(byte[] arrayToDeSerialize) {
object serializedObject;
using (MemoryStream stream = new MemoryStream(arrayToDeSerialize)) {
//Creating binary formatter to De-Serialize string.
BinaryFormatter formatter = new BinaryFormatter();
//De-Serializing.
serializedObject = formatter.Deserialize(stream);
}
return serializedObject;
}
}
Upvotes: 1
Reputation: 7525
If you want to use XmlSerializer you need to have a base class, not an interface.
XmlInclude is just one way to tell the serializer about possible implementations. Another approach is to actually pass included types into the serializer.
Simply enumerate all the types in your application that are derived from your base class (using reflection) and pass them into the serializer as an array of known types:
var serializer = new XmlSerializer(myBaseType, arrayOfConcreteImplementations);
exactly the same you can do with DataContractSerializer: second parameter is a collection of known types you can find using reflection), but using it you may be able to support interfaces...
Upvotes: 2
Reputation: 12025
I resolved this problem including a method named SerializeMyself in the Interface. I don't know if it's the most elegant solution... but it worked for me.
Upvotes: 0