StringSerializer.cs 1.4 KB

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