CharSerializer.cs 839 B

1234567891011121314151617181920212223242526272829303132
  1. #if !NO_RUNTIME
  2. using System;
  3. namespace ProtoBuf.Serializers
  4. {
  5. sealed class CharSerializer : UInt16Serializer
  6. {
  7. static readonly Type expectedType = typeof(char);
  8. public CharSerializer(ProtoBuf.Meta.TypeModel model) : base(model)
  9. {
  10. }
  11. public override Type ExpectedType => expectedType;
  12. public override void Write(object value, ProtoWriter dest)
  13. {
  14. ProtoWriter.WriteUInt16((ushort)(char)value, dest);
  15. }
  16. public override object Read(object value, ProtoReader source)
  17. {
  18. Helpers.DebugAssert(value == null); // since replaces
  19. return (char)source.ReadUInt16();
  20. }
  21. // no need for any special IL here; ushort and char are
  22. // interchangeable as long as there is no boxing/unboxing
  23. }
  24. }
  25. #endif