c# - protobuf-net 编码 DateTime UTC 解码 ToLocalTime

标签 c# .net datetime protobuf-net

有没有办法让 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/

相关文章:

.net - NetSparkle 与 SparkleDotNET

c# - 关于将实体映射到域对象的建议

c# - 更改默认字体的 AutoScaleMode 问题

c# - 您可以将接口(interface)附加到已定义的类吗

c# - .NET 5.0 独立 Azure 函数未触发代码 - 超时

python - 值错误 : time data '22.12.2012 17:00' does not match format '%d.%m.%Y %I:%M' , 上午/下午

c# - 堆已损坏。 C# dllimport、delphi PChar返回值

c# - 正则表达式问题,将数据提取到组

python - 有没有办法将不一致的字符串时间转换为python时间对象?

c++ - 如何根据当前语言环境格式设置在 SQLite 中格式化日期?