SystemTypeSerializer.cs 1.5 KB

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