C# 设置
Fory C# 提供 xlang 对象序列化、生成的模型以及 Fory gRPC。Apache.Fory NuGet 软件包需要 .NET 8 或更高版本,其中同时包含运行时和源代码生成器。
验证工具链
dotnet --version
对象序列化
创建控制台项目并添加已发布的软件包:
dotnet new console -n ForyExample
cd ForyExample
dotnet add package Apache.Fory --version 1.5.0
将 Program.cs 替换为:
using Apache.Fory;
[ForyStruct]
public sealed class User
{
public long Id { get; set; }
public string Name { get; set; } = string.Empty;
}
public static class Program
{
public static void Main()
{
Fory fory = Fory.Builder().Build();
fory.Register<User>(1);
byte[] bytes = fory.Serialize(new User { Id = 1, Name = "Alice" });
User decoded = fory.Deserialize<User>(bytes);
Console.WriteLine(decoded.Name);
}
}
dotnet run
C# 使用 xlang 模式。接下来可阅读 C# 对象序列化、xlang 类型、配置和 Schema 演进。