Skip to main content
Version: dev

Type Registration

This page covers how to register user types in Apache Fory™ C#.

Register by Numeric Type ID

Use explicit IDs for compact and stable cross-service mapping.

Fory fory = Fory.Builder().Build();
fory.Register<User>(100);
fory.Register<Order>(101);

Register by Type Name

Use name registration when you prefer symbolic mappings. The single-string overload accepts the full user-facing name and splits it at the last dot.

Fory fory = Fory.Builder().Build();
fory.Register<User>("com.example.User");

Names without dots use an empty namespace:

fory.Register<User>("User");

The split overload is also available when you already have the namespace and final type name separately:

fory.Register<User>("com.example", "User");

Register a Custom Serializer

Fory fory = Fory.Builder().Build();
fory.Register<MyType, MyTypeSerializer>(200);

Name-based custom serializer registration is also supported:

fory.Register<MyType, MyTypeSerializer>("com.example.MyType");

Thread-Safe Registration

ThreadSafeFory exposes the same registration APIs. Registrations are propagated to all per-thread Fory instances.

using ThreadSafeFory fory = Fory.Builder().BuildThreadSafe();
fory.Register<User>(100);
fory.Register<Order>(101);

Registration Rules

  • Register user-defined types on both writer and reader sides.
  • Keep ID/name mappings consistent across services and languages.
  • For external-type serialization, register the third-party target, such as fory.Register<ThirdParty.User>(100), not the local serializer declaration.
  • Register a concrete derived class by its concrete type. Annotated abstract bases and external declarations with BaseOnly = true are schema providers for descendants and are not registered.
  • Annotate every first-party class in a serializable hierarchy directly. Registering a derived class does not make an unannotated base class serializable.
  • For the split overloads, typeName must be non-empty and must not contain dots.
  • Register before high-volume serialization workloads to avoid missing type metadata.