BlobSerializer.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #if !NO_RUNTIME
  2. using System;
  3. #if COREFX
  4. using System.Reflection;
  5. #endif
  6. #if FEAT_COMPILER
  7. using System.Reflection.Emit;
  8. #endif
  9. #if FEAT_IKVM
  10. using Type = IKVM.Reflection.Type;
  11. #endif
  12. namespace ProtoBuf.Serializers
  13. {
  14. sealed class BlobSerializer : IProtoSerializer
  15. {
  16. public Type ExpectedType { get { return expectedType; } }
  17. #if FEAT_IKVM
  18. readonly Type expectedType;
  19. #else
  20. static readonly Type expectedType = typeof(byte[]);
  21. #endif
  22. public BlobSerializer(ProtoBuf.Meta.TypeModel model, bool overwriteList)
  23. {
  24. #if FEAT_IKVM
  25. expectedType = model.MapType(typeof(byte[]));
  26. #endif
  27. this.overwriteList = overwriteList;
  28. }
  29. private readonly bool overwriteList;
  30. #if !FEAT_IKVM
  31. public object Read(object value, ProtoReader source)
  32. {
  33. return ProtoReader.AppendBytes(overwriteList ? null : (byte[])value, source);
  34. }
  35. public void Write(object value, ProtoWriter dest)
  36. {
  37. ProtoWriter.WriteBytes((byte[])value, dest);
  38. }
  39. #endif
  40. bool IProtoSerializer.RequiresOldValue { get { return !overwriteList; } }
  41. bool IProtoSerializer.ReturnsValue { get { return true; } }
  42. #if FEAT_COMPILER
  43. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  44. {
  45. ctx.EmitBasicWrite("WriteBytes", valueFrom);
  46. }
  47. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  48. {
  49. if (overwriteList)
  50. {
  51. ctx.LoadNullRef();
  52. }
  53. else
  54. {
  55. ctx.LoadValue(valueFrom);
  56. }
  57. ctx.LoadReaderWriter();
  58. ctx.EmitCall(ctx.MapType(typeof(ProtoReader))
  59. .GetMethod("AppendBytes"));
  60. }
  61. #endif
  62. }
  63. }
  64. #endif