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