コード例 #1
0
        /// <summary>
        /// Deserializes a list.
        /// </summary>
        /// <param name="element">The <see cref="MemberBindingList"/> to parse.</param>
        /// <param name="state">The <see cref="SerializationState"/>.</param>
        /// <returns>The <see cref="MemberListBinding"/>.</returns>
        private MemberBinding DeserializeList(
            MemberBindingList element,
            SerializationState state)
        {
            var member = GetMemberFromKey(element.MemberKey);
            var auth   = new List <MemberInfo>
            {
                member,
            };
            var initializers = new List <ElementInit>();

            foreach (var init in element.Initializers)
            {
                var addMethodKey = init.AddMethodKey;
                typesCompressor.Value.DecompressTypes(
                    state.TypeIndex,
                    (addMethodKey, newKey => addMethodKey = newKey));
                var addMethod = GetMemberFromKey <MethodInfo>(addMethodKey);
                auth.Add(addMethod);
                initializers.Add(Expression.ElementInit(
                                     addMethod,
                                     init.Arguments.Select(arg => Serializer.Deserialize(arg, state))
                                     .ToArray()));
            }

            AuthorizeMembers(auth.ToArray());

            return(Expression.ListBind(member, initializers));
        }
コード例 #2
0
        /// <summary>
        /// Recurse the bindings for serialization.
        /// </summary>
        /// <param name="list">The list of <see cref="MemberBinding"/>.</param>
        /// <param name="state">The <see cref="SerializationState"/>.</param>
        /// <returns>The list of serialized bindings.</returns>
        private IEnumerable <object> SerializeBindings(IEnumerable <MemberBinding> list, SerializationState state)
        {
            var result = new List <object>();

            foreach (var binding in list)
            {
                switch (binding)
                {
                case MemberAssignment assignment:
                    var serializableBinding = new MemberBindingAssignment
                    {
                        Expression =
                            Serializer.Serialize(assignment.Expression, state),
                        MemberInfoKey = GetKeyForMember(assignment.Member),
                    };
                    result.Add(serializableBinding);
                    break;

                case MemberMemberBinding memberBinding:
                    var memberBindingSerializable = new MemberBindingMember
                    {
                        MemberKey = GetKeyForMember(memberBinding.Member),
                    };
                    memberBindingSerializable.Bindings.AddRange(
                        SerializeBindings(memberBinding.Bindings, state));
                    result.Add(memberBindingSerializable);
                    break;

                case MemberListBinding listBinding:
                    var memberListSerializable = new MemberBindingList
                    {
                        MemberKey =
                            GetKeyForMember(listBinding.Member),
                    };
                    foreach (var initializer in listBinding.Initializers)
                    {
                        var init = new MemberBindingInitializer
                        {
                            AddMethodKey = GetKeyForMember(initializer.AddMethod),
                        };
                        foreach (var argument in initializer.Arguments)
                        {
                            init.Arguments.Add(Serializer.Serialize(argument, state));
                        }

                        memberListSerializable.Initializers.Add(init);
                    }

                    result.Add(memberListSerializable);
                    break;
                }
            }

            return(result);
        }