[英]Binary Formatter - Type UserControl is not marked as serializable
I'm trying to serialize my object using BinaryFormatter
: 我正在尝试使用
BinaryFormatter
序列化我的对象:
private UserSettings _userSettings;
var serializer = new BinaryFormatter();
using (var file = new FileStream(@"D:\test.bin", FileMode.Create))
{
serializer.Serialize(file, _userSettings);
}
But I'm getting a strange exception: 但是我得到一个奇怪的例外:
Type 'MyCustomUserControl' in Assembly 'xxx' is not marked as serializable.
程序集“ xxx”中的类型“ MyCustomUserControl”未标记为可序列化。
Class UserSettings
doesn't have any reference to MyCustomUserControl
but it's referenced by MyCustomUserControl
. 类
UserSettings
没有对MyCustomUserControl
任何引用,但由MyCustomUserControl
引用。
The reference to the control can be hidden in an event. 控件的引用可以在事件中隐藏。 If your
UserSettings
class implements an event to which the control is subscribed, add the [field:NonSerialized]
attribute to the event: 如果您的
UserSettings
类实现了订阅了控件的事件,则将[field:NonSerialized]
属性添加到该事件:
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
For using BinaryFormatter
serializer, your class needs to be marked as Serializable
. 为了使用
BinaryFormatter
序列化程序,您的类需要标记为Serializable
。 Here is an example: 这是一个例子:
[Serializable]
public class MyCustomUserControl: Control
{
//
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.