Skip to content

Batch types registration

Vladyslav Taranov edited this page Jul 6, 2017 · 11 revisions

Use RuntimeTypeModel.Add(Assembly) or Add(Type[]) to add all types (marked with attributes) with all dependencies included.

This can be especially usable when you prepare your model for compilation.

Because subtypes have automatically generated field numbers (when not specified explicitly) it's required to export/import them if you want your RuntimeTypeModels to "understand" each other: Serialize(ExportTypeRelations()) - ImportTypeRelations(Deserialize()) . All the subtypes are imported in the correct order so they have the same field numbers as in their original model. Remember that the import/export functionality does not synchronize fields but only subtypes. The point of this is the fact that property numbers don't depend on the type adding order but subtype numbers do. The forceDefaultBehavior allows to add fields with their field numbers the same way like MetaType.ApplyDefaultBehavior() does. If you don't need to register any fields or want to add them manually pass false to this parameter.

As an alternative you can just add types in the same order for both RuntimeTypeModels or specify derived types field numbers with SerializeDerivedType attribute. RuntimeTypeModel.Add(Assembly) automatically sorts types by FullName so they are added in the same order each time.

Precompilation bakes subtype numbers into dll so import/export is only needed for RuntimeTypeModel.