有没有办法让 protobuf-net 以 UTC 自动编码 DateTime 值,但使用 ToLocalTime 解码?
我在不同时区的系统之间传递消息,并希望我的 DTO' 中的每个 DateTime 属性被序列化为 UTC(基本上在编码之前自动调用 ToUniversalTime)并在 LocalTime 中解码,以便自动接收消息的系统可以运行在 LocalTime 中的 DateTime 属性上,因此开发人员不必记住为通过线路接收的 DTO 在每个属性上调用 ToLocalTime?
最佳答案
对于这种类型的转换,通常的做法是有两个属性:
[ProtoMember(n, ...)]
public DateTime When { get; set; }
public DateTime WhenLocal => When.ToLocal(); // etc
序列化程序不会关心没有属性的那个。如果您真的不想调用者使用它,序列化程序属性可以是私有(private)的。
关于c# - protobuf-net 编码 DateTime UTC 解码 ToLocalTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48723723/