Int32Serializer.cs 1.4 KB

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