DecimalSerializer.cs 1.5 KB

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