【问题标题】:marking a WinForms Button as serializable将 WinForms 按钮标记为可序列化
【发布时间】:2010-08-30 02:24:14
【问题描述】:

这是我的第一个序列化程序。

尝试序列化按钮控件时出错。

public Form1()
{
     InitializeComponent();
     CheckSerialization();                
     Button btn = btnSerialized;            
}

public void CheckSerialization()
{
     Stream write = File.OpenWrite(@"C:\ser.bin");
     BinaryFormatter serial = new BinaryFormatter();
     serial.Serialize(write, btnSerialized);
     write.Close();
}

private void btnSerialized_Click(object sender, EventArgs e)
{
     FileStream fs = new FileStream(@"C:\ser.bin",FileMode.Open);
     BinaryFormatter bf= new BinaryFormatter();
     object obj = bf.Deserialize(fs);
     Button button12 = (Button)obj;
     button1 = button12;
     button1.Location = new Point(0, 0);
}

在程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中键入“System.Windows.Forms.Button”未标记为可序列化。**

如何将此对象标记为可序列化?

【问题讨论】:

标签: c# winforms serialization


【解决方案1】:

你没有。类型必须标记为可序列化,而不是对象。

【讨论】:

    【解决方案2】:

    找到类似于public partial class Form1 : Form 的行。 在它的正上方,放置[Serializable]。这标志着您的课程要进行序列化。 但是,您需要控制自己的序列化,因为如下所述,UI 对象不会序列化。为此,请查看ISerializable 接口。

    有关 SerializableAttribute 的更多信息是here

    【讨论】:

      【解决方案3】:

      你不能序列化 Winforms 对象(或其他 UI 对象,一般来说)

      【讨论】:

        【解决方案4】:

        如果您尝试导出对象或使用用户提供的动态属性值重新加载对象,那么为什么不使用 System.Reflection。这里:http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-26
          • 1970-01-01
          • 2017-02-21
          • 2015-11-10
          • 2016-01-18
          • 2011-01-17
          • 1970-01-01
          相关资源
          最近更新 更多