Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Formatter - Type UserControl is not marked as serializable

I'm trying to serialize my object using 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.

Class UserSettings doesn't have any reference to MyCustomUserControl but it's referenced by MyCustomUserControl.

like image 246
user2250152 Avatar asked Dec 14 '25 18:12

user2250152


1 Answers

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:

[field:NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
like image 78
Henrik Avatar answered Dec 17 '25 08:12

Henrik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!