protected override Func <PropertyInfo, Delegate> Call <TObj, TValue>() { ClassVisitor <DeserializeContext, TValue> elementVisitor = visitorFactory.GetClassVisitor <TValue>(); return(property => { ISetter <TObj, IList <TValue> > setter = Setter.Create <TObj, IList <TValue> >(property); return new ProcessField <DeserializeContext, TObj, IList <TValue> >( (DeserializeContext context, TObj obj, ref IList <TValue> value) => { value = new List <TValue>(); setter.Apply(obj, value); int length = PrimitiveDeserializer.Impl <int> .Instance(context.Stream); for (int i = 0; i < length; i++) { TValue element = new TValue(); if (elementVisitor.Visit(context, element) == VisitStatus.Exit) { return VisitStatus.Exit; } value.Add(element); } return VisitStatus.SkipChildren; }); }); }
protected override Func <PropertyInfo, Delegate> Call <TObj, TPrimitive>() { return(property => { ISetter <TObj, TPrimitive> setter = Setter.Create <TObj, TPrimitive>(property); return new ProcessField <DeserializeContext, TObj, TPrimitive>( (DeserializeContext context, TObj obj, ref TPrimitive value) => { value = Impl <TPrimitive> .Instance(context.Stream); setter.Apply(obj, value); return VisitStatus.SkipChildren; }); }); }
protected override Func <PropertyInfo, Delegate> Call <TObj, TValue>() { return(property => { ISetter <TObj, TValue> setter = Setter.Create <TObj, TValue>(property); return new ProcessField <DeserializeContext, TObj, TValue>( (DeserializeContext context, TObj obj, ref TValue value) => { value = new TValue(); setter.Apply(obj, value); return VisitStatus.Continue; }); }); }
public void GetterSetter() { { IGetter <MyClass, string> getter = Getter.Create <MyClass, string>(nameof(MyClass.MyStringProp)); ISetter <MyClass, string> setter = Setter.Create <MyClass, string>(nameof(MyClass.MyStringProp)); var obj = new MyClass(); Assert.Null(getter.Apply(obj)); setter.Apply(obj, "abc"); Assert.Equal("abc", getter.Apply(obj)); setter.Apply(obj, "def"); Assert.Equal("def", getter.Apply(obj)); } { IGetter <MyStruct, string> getter = Getter.Create <MyStruct, string>(nameof(MyStruct.MyStringProp)); ISetter <MyStruct, string> setter = Setter.Create <MyStruct, string>(nameof(MyStruct.MyStringProp)); var obj = new MyStruct(); Assert.Null(getter.Apply(obj)); setter.Apply(obj, "abc"); Assert.Equal("abc", getter.Apply(obj)); setter.Apply(obj, "def"); Assert.Equal("def", getter.Apply(obj)); } }
protected override Func <PropertyInfo, Delegate> Call <TObj>() { return(property => { ISetter <TObj, string> setter = Setter.Create <TObj, string>(property); return new ProcessField <DeserializeContext, TObj, string>( (DeserializeContext context, TObj obj, ref string value) => { int length = PrimitiveDeserializer.Impl <int> .Instance(context.Stream); byte[] bytes = new byte[length]; context.Stream.Read(bytes, 0, length); value = Marshallers <string> .Instance.FromBytes(bytes); setter.Apply(obj, value); return VisitStatus.SkipChildren; }); }); }
protected override Func <PropertyInfo, Delegate> Call <TObj, TStruct>() { int size = Unsafe.SizeOf <TStruct>(); return(property => { ISetter <TObj, TStruct> setter = Setter.Create <TObj, TStruct>(property); return new ProcessField <DeserializeContext, TObj, TStruct>( (DeserializeContext context, TObj obj, ref TStruct value) => { byte[] bytes = new byte[size]; context.Stream.Read(bytes, 0, size); value = Marshallers <TStruct> .Instance.FromBytes(bytes); setter.Apply(obj, value); return VisitStatus.SkipChildren; }); }); }