WCF DataContractSerializer namespace constructor issue RRS feed

  • Question

  • I have a class that I am manually cloning using the WCF DataContractSerializer in .NET 4.

    public object Clone()
       var serializer = new DataContractSerializer(GetType());       using (var ms = new System.IO.MemoryStream())                 {
          serializer.WriteObject(ms, this);
          ms.Position = 0;
          return serializer.ReadObject(ms);

    The class has a parameterless constructor which is implicitly called during serializer.ReadObject() - I have read answers on SO that state the DataContractSerializer does not call constructors but it definitely does in this instance.

    When I add a namespace to this class e.g.

    [DataContract(Namespace = "")]
    public class Blah

    this constructor will no longer be called.

    I have tried to create the DataContractSerializer with namespace parameters like so

    var serializer = new DataContractSerializer(GetType(), "blah", "");

    but is has no effect.

    My question is - how can I use the namespace and keep the constructor behavior?

    • Edited by bjames_dev Thursday, January 29, 2015 4:53 PM
    Thursday, January 29, 2015 4:51 PM


All replies