private static Func <T, object> CreateMemberGetter(ParameterExpression targetParameter, SerializingMember member)
        {
            var coreGetter =
                Expression.Lambda <Func <T, object> >(
                    Expression.Convert(
                        Expression.PropertyOrField(
                            targetParameter,
                            member.Member.Name
                            ),
                        typeof(object)
                        ),
                    targetParameter
                    ).Compile();

            if (member.Contract.NilImplication == NilImplication.Prohibit)
            {
                var name = member.Contract.Name;
                return(target =>
                {
                    var gotten = coreGetter(target);
                    if (gotten == null)
                    {
                        throw SerializationExceptions.NewNullIsProhibited(name);
                    }

                    return gotten;
                });
            }
            else
            {
                return(coreGetter);
            }
        }
        private void UnpackFromMap(Unpacker unpacker, ref object instance)
        {
            while (unpacker.Read())
            {
                var memberName = GetMemberName(unpacker);
                int index;
                if (!this._indexMap.TryGetValue(memberName, out index))
                {
                    // Drains unused value.
                    if (!unpacker.Read())
                    {
                        throw SerializationExceptions.NewUnexpectedEndOfStream();
                    }

                    // TODO: unknown member handling.

                    continue;
                }

                // Fetches value
                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewUnexpectedEndOfStream();
                }

                if (unpacker.LastReadData.IsNil)
                {
                    switch (this._nilImplications[index])
                    {
                    case NilImplication.Null:
                    {
                        this._memberSetters[index](ref instance, null);
                        continue;
                    }

                    case NilImplication.MemberDefault:
                    {
                        continue;
                    }

                    case NilImplication.Prohibit:
                    {
                        throw SerializationExceptions.NewNullIsProhibited(this._memberNames[index]);
                    }
                    }
                }

                if (unpacker.IsArrayHeader || unpacker.IsMapHeader)
                {
                    using (var subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        this.UnpackMemberInMap(subtreeUnpacker, ref instance, index);
                    }
                }
                else
                {
                    this.UnpackMemberInMap(unpacker, ref instance, index);
                }
            }
        }
Exemplo n.º 3
0
        private void HandleNilImplication(ref T instance, int index)
        {
            switch (this._nilImplications[index])
            {
            case NilImplication.Null:
            {
                if (this._memberSetters[index] == null)
                {
                    throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull(this._memberNames[index]);
                }

                this._memberSetters[index](ref instance, null);
                break;
            }

            case NilImplication.MemberDefault:
            {
                break;
            }

            case NilImplication.Prohibit:
            {
                throw SerializationExceptions.NewNullIsProhibited(this._memberNames[index]);
            }
            }
        }
 protected override Action <object> OnThrowNullIsProhibitedExceptionOnUnpacked(ReflectionSerializerNilImplicationHandlerOnUnpackedParameter parameter)
 {
     return
         (_ =>
     {
         throw SerializationExceptions.NewNullIsProhibited(parameter.MemberName);
     });
 }
 protected override Action <object> OnPackingCore(ReflectionSerializerNilImplicationHandlerParameter parameter, Func <object, bool> condition)
 {
     return
         (value =>
     {
         if (condition(value))
         {
             throw SerializationExceptions.NewNullIsProhibited(parameter.MemberName);
         }
     });
 }
        private void HandleNilImplication(ref object instance, int index)
        {
            switch (this._nilImplications[index])
            {
            case NilImplication.Null:
            {
                this._memberSetters[index](ref instance, null);
                break;
            }

            case NilImplication.MemberDefault:
            {
                break;
            }

            case NilImplication.Prohibit:
            {
                throw SerializationExceptions.NewNullIsProhibited(this._memberNames[index]);
            }
            }
        }
        private static Func <object, object> CreateMemberGetter(SerializingMember member)
        {
            Func <object, object> coreGetter;

            var fieldInfo = member.Member as FieldInfo;

            if (fieldInfo != null)
            {
                coreGetter = target => fieldInfo.GetValue(target);
            }
            else
            {
                var propertyInfo = member.Member as PropertyInfo;
                Contract.Assert(propertyInfo != null, member.ToString() + ":" + member.GetType());
                var propertyGetterInfo = propertyInfo.GetGetMethod();
                coreGetter = target => propertyGetterInfo.Invoke(target, new object[0]);
            }

            if (member.Contract.NilImplication == NilImplication.Prohibit)
            {
                var name = member.Contract.Name;
                return(target =>
                {
                    var gotten = coreGetter(target);
                    if (gotten == null)
                    {
                        throw SerializationExceptions.NewNullIsProhibited(name);
                    }

                    return gotten;
                });
            }
            else
            {
                return(coreGetter);
            }
        }