Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# [NonSerialized] field with inline initialization isn't initialized after deserialization

I have a [Nonserialized] field in my class that is initialized inline:

[NonSerialized]
private bool running = true;

However, after deserializing an object I have running == false. This is not what I want. Can I force inline initializatin to work for all [NonSerialized] fields? Otherwise I will have to implement ISerializable...

like image 415
Dmitry Risenberg Avatar asked Nov 17 '25 02:11

Dmitry Risenberg


1 Answers

You could set it in the default constructor.

Implement the System.Runtime.Serialization.IDeserializationCallback

It is called afther the object is deserialized so you can perform your extra initialization there .

like image 52
Stormenet Avatar answered Nov 19 '25 16:11

Stormenet