protobuf-net:如何在C#中表示DateTime? [英] protobuf-net : how to represent DateTime in C#?

查看:82
本文介绍了protobuf-net:如何在C#中表示DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protogen.exe long <类型的 proto2 消息字段生成此模式。 / code>:

protogen.exe generates this pattern for a proto2 message field of type long :

private long _Count = default(long);
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"Count", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
[global::System.ComponentModel.DefaultValue(default(long))]
public long Count
{
  get { return _Count; }
  set { _Count = value; }
}

但作为 proto2 不包含日期时间类型(并且 protobuf-net 不支持 proto3 其中包括 google.protobuf.Timestamp ),尚不清楚如何在手动编码的C#原型对象中表示 DateTime

but as proto2 does not include a date-time type ( and protobuf-net does not support proto3 which includes google.protobuf.Timestamp ) it's not clear how to represent DateTime in manually coded C# proto object.

这可能是错误的:

private DateTime _When = DateTime.MinValue;
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"When", DataFormat = global::ProtoBuf.DataFormat.Default)]
[global::System.ComponentModel.DefaultValue(DateTime.MinValue)]
public DateTime When
{
  get { return _When; }
  set { _When = value; }
}

装饰 DateTime <的正确方法是什么? / code>用于 protobuf-net 的属性?

推荐答案

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

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;}

这篇关于protobuf-net:如何在C#中表示DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆