跳到主要内容
版本:dev

Cross-Language Serialization

Apache Fory™ C# supports cross-language serialization with other Fory runtimes.

Enable Cross-Language Mode

C# defaults to Xlang(true), but it is good practice to configure it explicitly in interoperability code.

Fory fory = Fory.Builder()
.Xlang(true)
.Compatible(true)
.Build();

Register with Stable IDs

[ForyObject]
public sealed class Person
{
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
}

Fory fory = Fory.Builder()
.Xlang(true)
.Compatible(true)
.Build();

fory.Register<Person>(100);

Use the same ID mapping on all languages.

Register by Namespace/Type Name

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

Cross-Language Example

C# (Serializer)

Person person = new() { Name = "Alice", Age = 30 };
byte[] payload = fory.Serialize(person);

Java (Deserializer)

Fory fory = Fory.builder()
.withLanguage(Language.XLANG)
.withRefTracking(true)
.build();

fory.register(Person.class, 100);
Person value = (Person) fory.deserialize(payloadFromCSharp);

Python (Deserializer)

import pyfory

fory = pyfory.Fory(xlang=True, ref=True)
fory.register_type(Person, type_id=100)
value = fory.deserialize(payload_from_csharp)

Type Mapping Reference

See xlang guide for complete mapping.

Best Practices

  1. Keep type IDs stable and documented.
  2. Enable Compatible(true) for rolling upgrades.
  3. Register all user types on both read/write peers.
  4. Validate integration with real payload round trips.