且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

protobuf-net:如何在C#中表示DateTime?

更新时间:2023-02-03 13:52:16

这取决于您希望它在电线上的外观。如果您希望它的长度是 (进入时代),则:做到这一点。例如:

It depends on what you want it to look like on the wire. If you want it to be a long (delta into epoch), then : do that. For example:

[ProtoMember(...)] public long Foo {get;set;}

如果您希望它是的电线,而 DateTime 在您的代码中:这样做:

If you want it to be a long on the wire and a DateTime in your code: do that:

 public DateTime Foo {get;set;}
 [ProtoMember(...)] private long FooSerialized {
    get { return DateTimeToLong(Foo); }
    set { Foo = LongToDateTime(value); }
  }

如果您不在乎,只想存储 DateTime ,这样做:

If you don't care and just want to store a DateTime, do that:

[ProtoMember(...)] public DateTime Foo {get;set;}