【问题标题】:how to mark an object as Serializable如何将对象标记为可序列化
【发布时间】:2015-12-26 19:46:09
【问题描述】:

我有一个 system.document.table 对象,我想将该对象标记为可序列化以进行深度克隆。例外情况是 Type 'System.Windows.Documents.Table' in Assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 未标记为可序列化。

 FlowTable original =  new FlowTable();
 original = objFlowTab.deepclone();

objflowtab 在哪里有一个表对象

[Serializable]
public class FlowTable : Table
{ ....
  ....
  public static class ExtensionMethods
{
    // Deep clone
    public static T DeepClone<T>(this T a)
    { 

        using (MemoryStream stream = new MemoryStream())
        {

            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream,a);
            stream.Position = 0;
            return (T)formatter.Deserialize(stream);
        }
    }
}

我在 formatter.Serialize(stream,a); 中遇到错误

【问题讨论】:

  • 请在错误信息之外显示一些代码。
  • 这个应该先自己搜索一下就知道了。看起来你从来没有搜索过任何东西?
  • 确保您的对象没有任何无法序列化的成员(例如某些类型也没有Serializable 标记)。在某些情况下,您需要显式实现接口ISerializable
  • 坏主意。将表序列化为 XAML。我怀疑,二进制序列化在这里会起作用。

标签: c# wpf serializable documents cloning


【解决方案1】:

为要序列化的类添加[Serializable] 属性。

查看C#序列化详情here

【讨论】:

    【解决方案2】:

    对于可序列化的对象,您必须在类定义中包含 [Serializable]

    [Serializable]
    public class YourClass
    {
      //Your definition here;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-08
      • 2011-02-05
      • 2011-07-07
      • 1970-01-01
      • 2015-02-15
      • 1970-01-01
      • 2011-07-03
      • 2021-09-19
      • 1970-01-01
      相关资源
      最近更新 更多