views:

491

answers:

2

I have a generic type:

public class Packet<T> where T : IContent
{
    private int id;
    public int Id { get { return this.id; } }

    private T content;
    public T Content { get { return this.content; } }
}

I want to deserialize/serialize instances of this type from/to XML. IContent is defined like that:

public interface IContent
{
    XmlSerializer Serializer{get;}
}

Basically, I would like the Packet to use the serializer provided by its content to serialize and deserialize its content member. This serializer is in fact an instance of a pre-compiled xml serializer generated by sgen.exe.

Is it possible without making Packet<T> implementing IXmlSerializable?

+1  A: 

Yes, you can implement a custom class directly with IXmlSerializable.
For more information, see this article.

Sachin Gaur
My point is I would like to avoid custom serialization using IXmlSerializable. (See last question line.)
Romain Verdier
A: 

If you're using Generic Type, it is not able to generate pre-completed XmlSerializer. I have same problem.

Rock