I want to write a custom serializer for storing the session state into Azure Cache (Preview), so that means this custom serializer has to implement IDataCacheObjectSerializer (if I am wrong, please let me know). The reason why I need to write this custome serializer is i need to serialize some Linq objects which contain some System.Data.Linq.EntitySet attributes. And the default serializer NetDataContractSerializer cannot serialize the EntitySet.
My plan is to write a custom serializer by using DataContractSerializer (I searched some articles that mentioned DataContractSerializer can serialize EntitySet, so if I am wrong, let me know please), but the problem is how I can write a custom serializer that can support generic type.
Currently, my codes for custom serializer are like:
public class CustomSerializer : IDataCacheObjectSerializer
{
public CustomSerializer()
{ }
public object Deserialize(System.IO.Stream stream)
{
/* How can I know the type of object?????? */
DataContractSerializer dcs = new DataContractSerializer("Type of object");
}
public void Serialize(System.IO.Stream stream, object value)
{
DataContractSerializer dcs = new DataContractSerializer(value.GetType());
dcs.WriteObject(stream, value);
}
}
By the way, I have changed the Linq Serialization Mode to “Unidirectional”
I did not work with IDataCacheObjectSerializer, but common solution looks like this: