Int32Serializer.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #if !NO_RUNTIME
  2. using System;
  3. namespace ProtoBuf.Serializers
  4. {
  5. sealed class Int32Serializer : IProtoSerializer
  6. {
  7. static readonly Type expectedType = typeof(int);
  8. public Int32Serializer(ProtoBuf.Meta.TypeModel model) { }
  9. public Type ExpectedType => expectedType;
  10. bool IProtoSerializer.RequiresOldValue => false;
  11. bool IProtoSerializer.ReturnsValue => true;
  12. public object Read(object value, ProtoReader source)
  13. {
  14. Helpers.DebugAssert(value == null); // since replaces
  15. return source.ReadInt32();
  16. }
  17. public void Write(object value, ProtoWriter dest)
  18. {
  19. ProtoWriter.WriteInt32((int)value, dest);
  20. }
  21. #if FEAT_COMPILER
  22. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  23. {
  24. ctx.EmitBasicWrite("WriteInt32", valueFrom);
  25. }
  26. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  27. {
  28. ctx.EmitBasicRead("ReadInt32", ExpectedType);
  29. }
  30. #endif
  31. }
  32. }
  33. #endif