【问题标题】:C# help for serialization用于序列化的 C# 帮助
【发布时间】:2016-10-31 15:01:54
【问题描述】:

我对序列化有一个大问题。 我不知道问题出在哪里,但我可以在课堂上显示所有元素。

我用节点制作了图表。 但唯一的问题。

我使用接口来实现一种用于连接的元素。 例如 bool 可以与 bool 等连接。

我有

   [Serializable]
        public class Node : IElement
        {
          //More Constructor..
          public IEnumerable<NodeConnection>    Connections { get { return connections; } }

        public IEnumerable<NodeItem>        Items       { get { return nodeItems; } }

        public ElementType ElementType { get { return ElementType.Node; } }


        }

ElementType 是一个接口

使用节点:IElement

public interface IElement
    {
        ElementType ElementType { get; }
    }

在序列化时出现此错误

 BinaryFormatter bin = new BinaryFormatter();
                FileStream fs = new FileStream(sv.FileName, FileMode.Create, FileAccess.ReadWrite);

bin.Serialize(fs,graph.graphNode); //Error here

Assembly 'Graph, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 中的类型'Example' 未标记为可序列化。

序列化方法中的graph.graphNode是一个简单的List 有人有想法吗?

【问题讨论】:

  • 错误提及类型“示例”,请将其展示给我们,和/或确保它具有[Serializable] 属性。
  • 它说示例类型不是可序列化的市场。你有示例课吗?你能展示那堂课的内容吗?它是市场可序列化的吗?
  • 是的,它被标记为可序列化。看看我的示例序列化代码bin.Serialize(fs,graph.graphNode); 列表是这个public List&lt;Node&gt; graphNodes = new List&lt;Node&gt;(); 并且类被标记为可序列化查看顶部节点:IElement
  • 提供的代码没有提到这个Example 类。没有它,我们无能为力。
  • @Amy “示例”是 FormApplication,图表是 UserControl。和 graph.graphNode 是列表。从应用程序“示例”中,我只需编写一个 BinaryFormatter 并序列化 UserControl 中的所有元素。然后我有这个错误

标签: c# serialization


【解决方案1】:

如果表单名为 class Example {},那么您可能有一个或多个事件处理程序绑定到您的节点或 NodeItems。例如,如果 Node 和/或 NodeItem 实现 INotifyPropertyChanged 并且表单(示例类)绑定到 PropertyChanged,则 bin.Serialize() 尝试序列化整个表单。

解决方案:使用

[field: NonSerialized]

关于您想要序列化的类中的所有事件,如果它们被未被修复为序列化的类所消耗:

例如:

[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;

更准确地说:

  public class Example : Form
  {
    Node _node;

    public Example()
    {
      _node = new Node();
      _node.MyEvent += _node_MyEvent; // This will cause the BinaryFormatter to try to serialize Example form when serializing _node - unless [field: NonSerialized] attribute is used.
    }

    private void _node_MyEvent(object sender, EventArgs e)
    {

    }
  }

  [Serializable]
  public class Node
  {
    [field: NonSerialized]
    public event EventHandler MyEvent;
  }

编辑

如果一个可序列化的类引用一个不可序列化的对象也是如此:

[Serializable]
class Connection
{
   // Use [NonSerialized] attribute to prevent serialization of this reference:
   [NonSerialized]
   public Example; // A reference to a non serialized object
}

【讨论】:

  • 坦克 Henrik,一个问题已经解决,现在我无法序列化连接。看这个屏幕截图 (cdn.discordapp.com/attachments/207183378447990784/…) 这是我的图形控件。如果我不连接节点并对其进行序列化,则工作,否则如果我连接所有节点,我有相同的错误但变化很小,在 Assembly 'GraphNodes,Version=1.0.0.0,Culture=neutral,PublicKeyToken 中的 'GraphNodes.ExampleForm' =null' 未标记为可序列化。
  • @JoeMartiniello:我没有通过查看您的屏幕转储得到它。但我的猜测是,您可能在 Connection to ExampleForm 中有参考。请参阅上面的编辑。
猜你喜欢
  • 1970-01-01
  • 2012-02-20
  • 2011-03-08
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
相关资源
最近更新 更多