Wcf – Is it possible to serialize objects without a parameterless constructor in WCF


I know that a private parameterless constructor works but what about an object with no parameterless constructors?

I would like to expose types from a third party library so I have no control over the type definitions.

If there is a way what is the easiest? E.g. I don't what to have to create a sub type.


What I'm looking for is something like the level of customization shown here: http://msdn.microsoft.com/en-us/magazine/cc163902.aspx
although I don't want to have to resort to streams to serialize/deserialize.

Best Solution

You can't really make arbitrary types serializable; in some cases (XmlSerializer, for example) the runtime exposes options to spoof the attributes. But DataContractSerializer doesn't allow this. Feasible options:

  • hide the classes behind your own types that are serializable (lots of work)
  • provide binary formatter surrogates (yeuch)
  • write your own serialization core (a lot of work to get right)

Essentially, if something isn't designed for serialization, very little of the framework will let you serialize it.

Related Question