You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently when Jil determines the field addresses for optimal order, it will try to create an instance of the type using the constructor with the least amount of arguments. (See: Utils.cs line 467)
This will fail when the constructor does anything with the objects, for example:
I suggest using FormatterServices.GetUninitializedObject instead, this will create an instance of the object without calling any constructor, all fields will be initialized with its default value. The only downside to this is that the call will take about 11% longer than just using Activator.CreateInstance. A possible hybrid is to use Activator.CreateInstance when there is a default constructor, otherwise use FormatterServices.GetUninitializedObject.
This change allows objects that will never be deserialized anyway, be serialized without adding an empty constructor just for serialization.
I'm posting this as an issue so there is a place for discussion, I'm happy to post a pull request when you think this is an useful change @kevin-montrose.
The text was updated successfully, but these errors were encountered:
I would definitely opt for always using GetUninitializedObject. I've had issues with the default constructor "doing things" and then being inadvertently hit by deserialization (and, shockingly, even by serialization).
Currently when Jil determines the field addresses for optimal order, it will try to create an instance of the type using the constructor with the least amount of arguments. (See: Utils.cs line 467)
This will fail when the constructor does anything with the objects, for example:
I suggest using
FormatterServices.GetUninitializedObject
instead, this will create an instance of the object without calling any constructor, all fields will be initialized with its default value. The only downside to this is that the call will take about 11% longer than just usingActivator.CreateInstance
. A possible hybrid is to useActivator.CreateInstance
when there is a default constructor, otherwise useFormatterServices.GetUninitializedObject
.This change allows objects that will never be deserialized anyway, be serialized without adding an empty constructor just for serialization.
I'm posting this as an issue so there is a place for discussion, I'm happy to post a pull request when you think this is an useful change @kevin-montrose.
The text was updated successfully, but these errors were encountered: