ReflectedUriDecorator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #if !NO_RUNTIME
  2. #if PORTABLE
  3. using System;
  4. using System.Reflection;
  5. namespace ProtoBuf.Serializers
  6. {
  7. /// <summary>
  8. /// Manipulates with uris via reflection rather than strongly typed objects.
  9. /// This is because in PCLs, the Uri type may not match (WinRT uses Internal/Uri, .Net uses System/Uri)
  10. /// </summary>
  11. sealed class ReflectedUriDecorator : ProtoDecoratorBase
  12. {
  13. private readonly Type expectedType;
  14. private readonly PropertyInfo absoluteUriProperty;
  15. private readonly ConstructorInfo typeConstructor;
  16. public ReflectedUriDecorator(Type type, ProtoBuf.Meta.TypeModel model, IProtoSerializer tail) : base(tail)
  17. {
  18. expectedType = type;
  19. absoluteUriProperty = expectedType.GetProperty("AbsoluteUri");
  20. typeConstructor = expectedType.GetConstructor(new Type[] { typeof(string) });
  21. }
  22. public override Type ExpectedType { get { return expectedType; } }
  23. public override bool RequiresOldValue { get { return false; } }
  24. public override bool ReturnsValue { get { return true; } }
  25. public override void Write(object value, ProtoWriter dest)
  26. {
  27. Tail.Write(absoluteUriProperty.GetValue(value, null), dest);
  28. }
  29. public override object Read(object value, ProtoReader source)
  30. {
  31. Helpers.DebugAssert(value == null); // not expecting incoming
  32. string s = (string)Tail.Read(null, source);
  33. return s.Length == 0 ? null : typeConstructor.Invoke(new object[] { s });
  34. }
  35. #if FEAT_COMPILER
  36. protected override void EmitWrite(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  37. {
  38. ctx.LoadValue(valueFrom);
  39. ctx.LoadValue(absoluteUriProperty);
  40. Tail.EmitWrite(ctx, null);
  41. }
  42. protected override void EmitRead(Compiler.CompilerContext ctx, Compiler.Local valueFrom)
  43. {
  44. Tail.EmitRead(ctx, valueFrom);
  45. ctx.CopyValue();
  46. Compiler.CodeLabel @nonEmpty = ctx.DefineLabel(), @end = ctx.DefineLabel();
  47. ctx.LoadValue(typeof(string).GetProperty("Length"));
  48. ctx.BranchIfTrue(@nonEmpty, true);
  49. ctx.DiscardValue();
  50. ctx.LoadNullRef();
  51. ctx.Branch(@end, true);
  52. ctx.MarkLabel(@nonEmpty);
  53. ctx.EmitCtor(expectedType, ctx.MapType(typeof(string)));
  54. ctx.MarkLabel(@end);
  55. }
  56. #endif
  57. }
  58. }
  59. #endif
  60. #endif