BlobSerializer.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. namespace ProtoBuf.Serializers
  10. {
  11. sealed class BlobSerializer : IProtoSerializer
  12. {
  13. public Type ExpectedType { get { return expectedType; } }
  14. static readonly Type expectedType = typeof(byte[]);
  15. public BlobSerializer(ProtoBuf.Meta.TypeModel model, bool overwriteList)
  16. {
  17. this.overwriteList = overwriteList;
  18. }
  19. private readonly bool overwriteList;
  20. public object Read(object value, ProtoReader source)
  21. {
  22. return ProtoReader.AppendBytes(overwriteList ? null : (byte[])value, source);
  23. }
  24. public void Write(object value, ProtoWriter dest)
  25. {
  26. ProtoWriter.WriteBytes((byte[])value, dest);
  27. }
  28. bool IProtoSerializer.RequiresOldValue { get { return !overwriteList; } }
  29. bool IProtoSerializer.ReturnsValue { get { return true; } }
  30. #if FEAT_COMPILER
  31. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  32. {
  33. ctx.EmitBasicWrite("WriteBytes", valueFrom);
  34. }
  35. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  36. {
  37. if (overwriteList)
  38. {
  39. ctx.LoadNullRef();
  40. }
  41. else
  42. {
  43. ctx.LoadValue(valueFrom);
  44. }
  45. ctx.LoadReaderWriter();
  46. ctx.EmitCall(ctx.MapType(typeof(ProtoReader))
  47. .GetMethod("AppendBytes"));
  48. }
  49. #endif
  50. }
  51. }
  52. #endif