StringSerializer.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #if !NO_RUNTIME
  2. using System;
  3. using ProtoBuf.Meta;
  4. #if FEAT_IKVM
  5. using Type = IKVM.Reflection.Type;
  6. using IKVM.Reflection;
  7. #else
  8. using System.Reflection;
  9. #endif
  10. namespace ProtoBuf.Serializers
  11. {
  12. sealed class StringSerializer : IProtoSerializer
  13. {
  14. #if FEAT_IKVM
  15. readonly Type expectedType;
  16. #else
  17. static readonly Type expectedType = typeof(string);
  18. #endif
  19. public StringSerializer(ProtoBuf.Meta.TypeModel model)
  20. {
  21. #if FEAT_IKVM
  22. expectedType = model.MapType(typeof(string));
  23. #endif
  24. }
  25. public Type ExpectedType { get { return expectedType; } }
  26. public void Write(object value, ProtoWriter dest)
  27. {
  28. ProtoWriter.WriteString((string)value, dest);
  29. }
  30. bool IProtoSerializer.RequiresOldValue { get { return false; } }
  31. bool IProtoSerializer.ReturnsValue { get { return true; } }
  32. public object Read(object value, ProtoReader source)
  33. {
  34. Helpers.DebugAssert(value == null); // since replaces
  35. return source.ReadString();
  36. }
  37. #if FEAT_COMPILER
  38. void IProtoSerializer.EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  39. {
  40. ctx.EmitBasicWrite("WriteString", valueFrom);
  41. }
  42. void IProtoSerializer.EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  43. {
  44. ctx.EmitBasicRead("ReadString", ExpectedType);
  45. }
  46. #endif
  47. }
  48. }
  49. #endif