DateTimeSerializer.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #if !NO_RUNTIME
  2. using System;
  3. #if FEAT_IKVM
  4. using Type = IKVM.Reflection.Type;
  5. using IKVM.Reflection;
  6. #else
  7. using System.Reflection;
  8. #endif
  9. namespace ProtoBuf.Serializers
  10. {
  11. sealed class DateTimeSerializer : IProtoSerializer
  12. {
  13. #if FEAT_IKVM
  14. readonly Type expectedType;
  15. #else
  16. static readonly Type expectedType = typeof(DateTime);
  17. #endif
  18. public Type ExpectedType { get { return expectedType; } }
  19. bool IProtoSerializer.RequiresOldValue { get { return false; } }
  20. bool IProtoSerializer.ReturnsValue { get { return true; } }
  21. private readonly bool includeKind;
  22. public DateTimeSerializer(ProtoBuf.Meta.TypeModel model)
  23. {
  24. #if FEAT_IKVM
  25. expectedType = model.MapType(typeof(DateTime));
  26. #endif
  27. includeKind = model != null && model.SerializeDateTimeKind();
  28. }
  29. #if !FEAT_IKVM
  30. public object Read(object value, ProtoReader source)
  31. {
  32. Helpers.DebugAssert(value == null); // since replaces
  33. return BclHelpers.ReadDateTime(source);
  34. }
  35. public void Write(object value, ProtoWriter dest)
  36. {
  37. if(includeKind)
  38. BclHelpers.WriteDateTimeWithKind((DateTime)value, dest);
  39. else
  40. BclHelpers.WriteDateTime((DateTime)value, dest);
  41. }
  42. #endif
  43. #if FEAT_COMPILER
  44. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  45. {
  46. ctx.EmitWrite(ctx.MapType(typeof(BclHelpers)), includeKind ? "WriteDateTimeWithKind" : "WriteDateTime", valueFrom);
  47. }
  48. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  49. {
  50. ctx.EmitBasicRead(ctx.MapType(typeof(BclHelpers)), "ReadDateTime", ExpectedType);
  51. }
  52. #endif
  53. }
  54. }
  55. #endif