Serialization of derived FixedDocument

会有一股神秘感。 提交于 2019-12-24 04:14:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/32474210/serialization-of-derived-fixeddocument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!