コード例 #1
0
        protected virtual void AddItem(T collection, MessagePackObject item)
        {
            if (this._addItem == null)
            {
                throw SerializationExceptions.NewUnpackToIsNotSupported(typeof(T), null);
            }

            try
            {
                this._addItem.Invoke(collection, new object[] { item });
            }
            catch (TargetInvocationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw SerializationExceptions.NewUnpackToIsNotSupported(typeof(T), ex);
            }
        }
コード例 #2
0
        protected internal override void UnpackToCore(Unpacker unpacker, T collection)
        {
            if (this._collectionDeserializer != null)
            {
                // Fast path:
                this._collectionDeserializer.UnpackTo(unpacker, collection);
            }
            else
            {
                if (this._addItem == null)
                {
                    throw SerializationExceptions.NewUnpackToIsNotSupported(typeof(T), null);
                }

                if (!unpacker.IsArrayHeader)
                {
                    throw SerializationExceptions.NewIsNotArrayHeader();
                }

                this.UnpackToCore(unpacker, collection, UnpackHelpers.GetItemsCount(unpacker));
            }
        }
コード例 #3
0
        private async Task UnpackAndAddCollectionItemAsync(object objectGraph, Unpacker unpacker, int index, CancellationToken cancellationToken)
        {
            var destination = this._getters[index](objectGraph);

            if (destination == null)
            {
                throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull(this._contracts[index].Name);
            }

            var traits = destination.GetType().GetCollectionTraits(CollectionTraitOptions.WithAddMethod, this.OwnerContext.CompatibilityOptions.AllowNonCollectionEnumerableTypes);

            if (traits.AddMethod == null)
            {
                throw SerializationExceptions.NewUnpackToIsNotSupported(destination.GetType(), null);
            }

            var source = await this._serializers[index].UnpackFromAsync(unpacker, cancellationToken).ConfigureAwait(false) as IEnumerable;

            if (source != null)
            {
                switch (traits.DetailedCollectionType)
                {
                case CollectionDetailedKind.GenericDictionary:
                    {
                        // item should be KeyValuePair<TKey, TValue>
                        var arguments = new object[2];
                        var key       = default(PropertyInfo);
                        var value     = default(PropertyInfo);
                        foreach (var item in source)
                        {
                            if (key == null)
                            {
                                key   = item.GetType().GetProperty("Key");
                                value = item.GetType().GetProperty("Value");
                            }

                            arguments[0] = key.GetValue(item, null);
                            arguments[1] = value.GetValue(item, null);
                            traits.AddMethod.InvokePreservingExceptionType(destination, arguments);
                        }
                        break;
                    }

                case CollectionDetailedKind.NonGenericDictionary:
                    {
                        // item should be DictionaryEntry
                        var arguments = new object[2];
                        foreach (var item in source)
                        {
                            arguments[0] = ReflectionSerializerHelper.DictionaryEntryKeyProperty.GetValue(item, null);
                            arguments[1] = ReflectionSerializerHelper.DictionaryEntryValueProperty.GetValue(item, null);
                            traits.AddMethod.InvokePreservingExceptionType(destination, arguments);
                        }
                        break;
                    }

                default:
                    {
                        var arguments = new object[1];
                        foreach (var item in source)
                        {
                            arguments[0] = item;
                            traits.AddMethod.InvokePreservingExceptionType(destination, arguments);
                        }
                        break;
                    }
                }
            }
        }         // UnpackAndAddCollectionItemAsync
 protected virtual void AddItem(object collection, object item)
 {
     throw SerializationExceptions.NewUnpackToIsNotSupported(this.TargetType, null);
 }
 /// <summary>
 ///		When implemented by derive class,
 ///		adds the deserialized item to the collection on <typeparamref name="TCollection"/> specific manner
 ///		to implement <see cref="UnpackToCore(Unpacker,TCollection)"/>.
 /// </summary>
 /// <param name="collection">The collection to be added.</param>
 /// <param name="item">The item to be added.</param>
 /// <exception cref="NotSupportedException">
 ///		This implementation always throws it.
 /// </exception>
 protected virtual void AddItem(TCollection collection, object item)
 {
     throw SerializationExceptions.NewUnpackToIsNotSupported(typeof(TCollection), null);
 }
コード例 #6
0
        private void UnpackAndAddCollectionItem(object objectGraph, Unpacker unpacker, int index)
        {
            var destination = this._getters[index](objectGraph);

            if (destination == null)
            {
                throw SerializationExceptions.NewReadOnlyMemberItemsMustNotBeNull(this._contracts[index].Name);
            }

            var traits = destination.GetType().GetCollectionTraits();

            if (traits.AddMethod == null)
            {
                throw SerializationExceptions.NewUnpackToIsNotSupported(destination.GetType(), null);
            }

            var source = this._serializers[index].UnpackFrom(unpacker) as IEnumerable;

            if (source != null)
            {
                switch (traits.DetailedCollectionType)
                {
                case CollectionDetailedKind.GenericDictionary:
                    {
                        // item should be KeyValuePair<TKey, TValue>
                        var arguments = new object[2];
                        var key       = default(PropertyInfo);
                        var value     = default(PropertyInfo);
                        foreach (var item in source)
                        {
                            if (key == null)
                            {
                                key   = item.GetType().GetProperty("Key");
                                value = item.GetType().GetProperty("Value");
                            }

                            arguments[0] = key.GetValue(item, null);
                            arguments[1] = value.GetValue(item, null);
                            traits.AddMethod.InvokePreservingExceptionType(destination, arguments);
                        }
                        break;
                    }

                case CollectionDetailedKind.NonGenericDictionary:
                    {
                        // item should be DictionaryEntry
                        var arguments = new object[2];
                        foreach (var item in source)
                        {
                            arguments[0] = DictionaryEntryKeyProperty.GetValue(item, null);
                            arguments[1] = DictionaryEntryValueProperty.GetValue(item, null);
                            traits.AddMethod.InvokePreservingExceptionType(destination, arguments);
                        }
                        break;
                    }

                default:
                    {
                        var arguments = new object[1];
                        foreach (var item in source)
                        {
                            arguments[0] = item;
                            traits.AddMethod.InvokePreservingExceptionType(destination, arguments);
                        }
                        break;
                    }
                }
            }
        }
コード例 #7
0
		/// <summary>
		///		When implemented by derive class, 
		///		adds the deserialized item to the collection on <typeparamref name="TDictionary"/> specific manner
		///		to implement <see cref="UnpackToCore(Unpacker,TDictionary)"/>.
		/// </summary>
		/// <param name="dictionary">The dictionary to be added.</param>
		/// <param name="key">The key to be added.</param>
		/// <param name="value">The value to be added.</param>
		/// <exception cref="NotSupportedException">
		///		This implementation always throws it.
		/// </exception>
		protected virtual void AddItem( TDictionary dictionary, TKey key, TValue value )
		{
			throw SerializationExceptions.NewUnpackToIsNotSupported( typeof( TDictionary ), null );
		}