我正在尝试使用 BinaryFormatter
序列化我的对象:
private UserSettings _userSettings;
var serializer = new BinaryFormatter();
using (var file = new FileStream(@"D:\test.bin", FileMode.Create))
{
serializer.Serialize(file, _userSettings);
}
但是我遇到了一个奇怪的异常:
Type 'MyCustomUserControl' in Assembly 'xxx' is not marked as serializable.
UserSettings
类没有对 MyCustomUserControl
的任何引用,但它被 MyCustomUserControl
引用。
最佳答案
对控件的引用可以隐藏在事件中。如果您的 UserSettings
类实现了控件订阅的事件,请将 [field:NonSerialized]
属性添加到该事件:
[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
关于c# - 二进制格式化程序 - 类型 UserControl 未标记为可序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40913641/