之前之后我创建了一个类现在获取序列化的错误,即使插入[Serializable接口]
[Serializable]
public class clsCategories
{
public static List<infoCategories> listCategories = new List<infoCategories>();
public void serialize()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream fs = new FileStream("categories.dat", FileMode.Create);
bf.Serialize(fs, listCategories);
fs.Close();
}
[Serializable]
public class infoCategories
{
public PictureBox img { get; set; }
public Label lbl { get; set; }
}
}
调用此方法时...
private void btnDone_Click(object sender, EventArgs e)
{
objCategories.serialize();
this.Hide();
}
我得到这个错误:
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: Type 'System.Windows.Forms.PictureBox' in Assembly 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
我错在哪里?
所有成员都必须是可序列化。 'PictureBox'和'Label'不是。 –
@IvanStoev但我写了 [Serializable] 高于他们的类。我仍然错过了什么?如果是的话,告诉我 – ShoaibSivany
你可以序列化一个位图,或者你可以创建一个类SerilizablePictureBox。只需添加属性并不总是足够的。 – TaW