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 member field numbers but only subtypes. The point of this is the fact that member field numbers don't depend on the type adding order but subtype numbers do. The forceDefaultBehavior allows to add members with their field numbers the same way as MetaType.ApplyDefaultBehavior() does. If you don't need to register any members 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.