Skip to content

Changing auto add behavior

Vladyslav Taranov edited this page Nov 29, 2019 · 17 revisions

When AqlaSerializer meets unknown type it tries to recognize it automatically the same way as RuntimeTypeModel.Add(type, true) does.

You can get or set the current IAutoAddStrategy with RuntimeTypeModel.AutoAddStrategy. The default strategy can be casted to AutoAddStrategy so you can alter its settings. To avoid unpredictable side effects please don't try hot-swapping AutoAddStrategy or changing its settings between adding related (via inheritance or composition) types.

Subtypes

By default subtypes are automatically registered to their base types as with SerializeDerivedType attribute (if they were not registered before). They have automatic fields numbers assigned starting from AutoRegisteringSubtypesFirstTag. You may set DisableAutoRegisteringSubtypes to disable this behavior.

Serializable attribute

You can enable System.Serializable attribute support by modifying AcceptableAttributes property. For this attribute ImplicitFields.AllFields mode is used.

Other settings

Please read xmldocs on AutoAddStrategy using your IDE IntelliSense.

One of important settings is AutoAddStrategy.ImplicitFallbackMode which allows you to setup ImplicitFields behavior for not recognized types.

Extending auto-add behavior

To alter attributes mapping or add a support for new attributes you can set properties TypeMapper and MemberMapper on AutoAddStrategy.

Both mappers are also extendable, you can pass your custom handlers to their default implementation constructors. To get default handlers list use CreateDefault(Type|Member)MapperHandlers on AutoAddStrategy. Please see the source code to understand how to implement your custom handlers.

Also you may implement your own custom IAutoAddStrategy; note that it's required to inherit AutoAddStrategy so you have an access to required protected methods.

In previous versions the default implementation of IAutoAddStrategy had DefaultAutoAddStrategy class name.