ByteSerializer.cs 1.4 KB

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