Search code examples
c#wpfserializationxps

Serialization of derived FixedDocument


Since you can only add pages to a FixedDocument, I wrote a derived class:

public class CustomFixedDocument : FixedDocument
{
    public void RemoveChild(object child)
    {
        base.RemoveLogicalChild(child);
    }
}

to replace FixedDocument, which works fine, until I try to print the document and receive the following error:

An unhandled exception of type 'System.Windows.Xps.XpsSerializationException' occurred in ReachFramework.dll

Additional information: Serialization of this type of object is not supported.

I haven't worked with serialization that much in the past and have read up on it but still can't solve the issues. I have also tried the

[Serializable]

attribute, but it doesn't make any difference.

Can anybody guide me in the correct direction or have any ideas what to do?


Solution

  • If you look at decompiled source code of the method which checks if certain type is supported, you will see roughly the following:

    internal bool IsSerializedObjectTypeSupported(object serializedObject)
    {
      bool flag = false;
      Type type = serializedObject.GetType();
      if (this._isBatchMode)
      {
        if (typeof (Visual).IsAssignableFrom(type) && type != typeof (FixedPage))
          flag = true;
      }
      else if (type == typeof (FixedDocumentSequence) || type == typeof (FixedDocument) || (type == typeof (FixedPage) || typeof (Visual).IsAssignableFrom(type)) || typeof (DocumentPaginator).IsAssignableFrom(type))
        flag = true;
      return flag;
    }
    

    Here you see that this type should either inherit DocumentPaginator, Visual, or be exactly of type FixedDocument, FixedDocumentSequence, FixedPage. So, types inherited from FixedDocument will not work, whatever serializable attributes you will use, so you have to find a different approach. I think that is a bug in XpsSerializationManager, but maybe there is some deep reason.