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