BooleanSerializer.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 BooleanSerializer : IProtoSerializer
  12. {
  13. #if FEAT_IKVM
  14. readonly Type expectedType;
  15. #else
  16. static readonly Type expectedType = typeof(bool);
  17. #endif
  18. public BooleanSerializer(ProtoBuf.Meta.TypeModel model)
  19. {
  20. #if FEAT_IKVM
  21. expectedType = model.MapType(typeof(bool));
  22. #endif
  23. }
  24. public Type ExpectedType { get { return expectedType; } }
  25. #if !FEAT_IKVM
  26. public void Write(object value, ProtoWriter dest)
  27. {
  28. ProtoWriter.WriteBoolean((bool)value, dest);
  29. }
  30. public object Read(object value, ProtoReader source)
  31. {
  32. Helpers.DebugAssert(value == null); // since replaces
  33. return source.ReadBoolean();
  34. }
  35. #endif
  36. bool IProtoSerializer.RequiresOldValue { get { return false; } }
  37. bool IProtoSerializer.ReturnsValue { get { return true; } }
  38. #if FEAT_COMPILER
  39. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  40. {
  41. ctx.EmitBasicWrite("WriteBoolean", valueFrom);
  42. }
  43. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  44. {
  45. ctx.EmitBasicRead("ReadBoolean", ExpectedType);
  46. }
  47. #endif
  48. }
  49. }
  50. #endif