CharSerializer.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #if !NO_RUNTIME
  2. using System;
  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 CharSerializer : UInt16Serializer
  12. {
  13. #if FEAT_IKVM
  14. readonly Type expectedType;
  15. #else
  16. static readonly Type expectedType = typeof(char);
  17. #endif
  18. public CharSerializer(ProtoBuf.Meta.TypeModel model) : base(model)
  19. {
  20. #if FEAT_IKVM
  21. expectedType = model.MapType(typeof(char));
  22. #endif
  23. }
  24. public override Type ExpectedType { get { return expectedType; } }
  25. #if !FEAT_IKVM
  26. public override void Write(object value, ProtoWriter dest)
  27. {
  28. ProtoWriter.WriteUInt16((ushort)(char)value, dest);
  29. }
  30. public override object Read(object value, ProtoReader source)
  31. {
  32. Helpers.DebugAssert(value == null); // since replaces
  33. return (char)source.ReadUInt16();
  34. }
  35. #endif
  36. // no need for any special IL here; ushort and char are
  37. // interchangeable as long as there is no boxing/unboxing
  38. }
  39. }
  40. #endif