GuidSerializer.cs 1.4 KB

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