SingleSerializer.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 SingleSerializer : IProtoSerializer
  13. {
  14. #if FEAT_IKVM
  15. readonly Type expectedType;
  16. #else
  17. static readonly Type expectedType = typeof(float);
  18. #endif
  19. public Type ExpectedType { get { return expectedType; } }
  20. public SingleSerializer(TypeModel model)
  21. {
  22. #if FEAT_IKVM
  23. expectedType = model.MapType(typeof(float));
  24. #endif
  25. }
  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 source.ReadSingle();
  33. }
  34. public void Write(object value, ProtoWriter dest)
  35. {
  36. ProtoWriter.WriteSingle((float)value, dest);
  37. }
  38. #endif
  39. #if FEAT_COMPILER
  40. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  41. {
  42. ctx.EmitBasicWrite("WriteSingle", valueFrom);
  43. }
  44. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  45. {
  46. ctx.EmitBasicRead("ReadSingle", ExpectedType);
  47. }
  48. #endif
  49. }
  50. }
  51. #endif