internal static MessagePackSerializer <T> CreateReflectionInternal <T>(SerializationContext context, Type concreteType, PolymorphismSchema schema)
        {
            if (concreteType.GetIsAbstract() || concreteType.GetIsInterface())
            {
                // return null for polymoirphic provider.
                return(null);
            }

            var serializer = context.Serializers.Get <T>(context);

            if (serializer != null)
            {
                // For MessagePack.Create compatibility.
                // Required for built-in types.
                return(serializer);
            }

            ValidateType(typeof(T));
            var traits = typeof(T).GetCollectionTraits();

            switch (traits.CollectionType)
            {
            case CollectionKind.Array:
            case CollectionKind.Map:
            {
                return
                    (#if !UNITY
                     ReflectionSerializerHelper.CreateCollectionSerializer <T>(context, concreteType, traits, (schema ?? PolymorphismSchema.Default)));
#else
                     Wrap <T>(
                         context,
                         ReflectionSerializerHelper.CreateCollectionSerializer <T>(context, concreteType, traits, (schema ?? PolymorphismSchema.Default))
                         );
#endif // !UNITY
            }

            default:
            {
                if (typeof(T).GetIsEnum())
                {
                    return(ReflectionSerializerHelper.CreateReflectionEnumMessagePackSerializer <T>(context));
                }
#if !WINDOWS_PHONE && !NETFX_35 && !UNITY
                if (TupleItems.IsTuple(typeof(T)))
                {
                    return
                        (new ReflectionTupleMessagePackSerializer <T>(
                             context,
                             (schema ?? PolymorphismSchema.Default).ChildSchemaList
                             ));
                }
#endif // !WINDOWS_PHONE && !NETFX_35 && !UNITY

                return(new ReflectionObjectMessagePackSerializer <T>(context));
            }
            }
        }
        /// <summary>
        ///		Builds the serializer and returns its new instance.
        /// </summary>
        /// <param name="context">The context information. This value will not be <c>null</c>.</param>
        /// <param name="concreteType">The substitution type if <see cref="TargetType"/> is abstract type. <c>null</c> when <see cref="TargetType"/> is not abstract type.</param>
        /// <param name="schema">The schema which contains schema for collection items, dictionary keys, or tuple items. This value may be <c>null</c>.</param>
        /// <param name="targetInfo">The parsed serialization target information.</param>
        /// <returns>
        ///		Newly created serializer object.
        ///		This value will not be <c>null</c>.
        /// </returns>
        protected void BuildSerializer(TContext context, Type concreteType, PolymorphismSchema schema, out SerializationTarget targetInfo)
        {
#if DEBUG
            Contract.Assert(!this.TargetType.IsArray);
            Contract.Assert(!this.TargetType.GetIsEnum());
#endif

            switch (this.CollectionTraits.CollectionType)
            {
            case CollectionKind.Array:
            case CollectionKind.Map:
            {
                targetInfo = null;
                this.BuildCollectionSerializer(context, concreteType, schema);
                break;
            }

            case CollectionKind.NotCollection:
            {
                var nullableUnderlyingType = Nullable.GetUnderlyingType(this.TargetType);
                if (nullableUnderlyingType != null)
                {
                    targetInfo = null;
                    this.BuildNullableSerializer(context, nullableUnderlyingType);
                }
#if !NETFX_35
                else if (TupleItems.IsTuple(this.TargetType))
                {
                    this.BuildTupleSerializer(context, (schema ?? PolymorphismSchema.Default).ChildSchemaList, out targetInfo);
                }
#endif
                else
                {
#if DEBUG
                    Contract.Assert(schema == null || schema.UseDefault);
#endif // DEBUG
                    this.BuildObjectSerializer(context, out targetInfo);
                }
                break;
            }

            default:
            {
                throw new NotSupportedException("Unknown traits :" + this.CollectionTraits);
            }
            }
        }
        /// <summary>
        ///		Builds the serializer and returns its new instance.
        /// </summary>
        /// <param name="context">The context information. This value will not be <c>null</c>.</param>
        /// <param name="concreteType">The substitution type if <typeparamref name="TObject"/> is abstract type. <c>null</c> when <typeparamref name="TObject"/> is not abstract type.</param>
        /// <param name="schema">The schema which contains schema for collection items, dictionary keys, or tuple items. This value may be <c>null</c>.</param>
        /// <returns>
        ///		Newly created serializer object.
        ///		This value will not be <c>null</c>.
        /// </returns>
        protected void BuildSerializer(TContext context, Type concreteType, PolymorphismSchema schema)
        {
#if DEBUG
            Contract.Assert(!typeof(TObject).IsArray);
            Contract.Assert(!typeof(TObject).GetIsEnum());
#endif

            switch (CollectionTraitsOfThis.CollectionType)
            {
            case CollectionKind.Array:
            case CollectionKind.Map:
            {
                this.BuildCollectionSerializer(context, concreteType, schema);
                break;
            }

            case CollectionKind.NotCollection:
            {
                var nullableUnderlyingType = Nullable.GetUnderlyingType(typeof(TObject));
                if (nullableUnderlyingType != null)
                {
                    this.BuildNullableSerializer(context, nullableUnderlyingType);
                }
#if !NETFX_35
                else if (TupleItems.IsTuple(typeof(TObject)))
                {
                    this.BuildTupleSerializer(context, (schema ?? PolymorphismSchema.Default).ChildSchemaList);
                }
#endif
                else
                {
#if DEBUG && !UNITY
                    Contract.Assert(schema == null || schema.UseDefault);
#endif // DEBUG
                    this.BuildObjectSerializer(context);
                }
                break;
            }
            }
        }
Beispiel #4
0
        internal static MessagePackSerializer <T> CreateReflectionInternal <T>(SerializationContext context, Type concreteType, PolymorphismSchema schema)
        {
            if (concreteType.GetIsAbstract() || concreteType.GetIsInterface())
            {
                // return null for polymoirphic provider.
                return(null);
            }

            var serializer = context.Serializers.Get <T>(context);

            if (serializer != null)
            {
                // For MessagePack.Create compatibility.
                // Required for built-in types.
                return(serializer);
            }

            ValidateType(typeof(T));
            var traits =
#if !UNITY
                typeof(T).GetCollectionTraits(CollectionTraitOptions.WithAddMethod, context.CompatibilityOptions.AllowNonCollectionEnumerableTypes);
#else
                typeof(T).GetCollectionTraits(CollectionTraitOptions.WithAddMethod | CollectionTraitOptions.WithCountPropertyGetter, context.CompatibilityOptions.AllowNonCollectionEnumerableTypes);
#endif
            switch (traits.CollectionType)
            {
            case CollectionKind.Array:
            case CollectionKind.Map:
            {
                return
                    (#if !UNITY
                     ReflectionSerializerHelper.CreateCollectionSerializer <T>(context, concreteType, traits, (schema ?? PolymorphismSchema.Default)));
#else
                     Wrap <T>(
                         context,
                         ReflectionSerializerHelper.CreateCollectionSerializer <T>(context, concreteType, traits, (schema ?? PolymorphismSchema.Default))
                         );
#endif // !UNITY
            }

            default:
            {
                if (typeof(T).GetIsEnum())
                {
                    return(ReflectionSerializerHelper.CreateReflectionEnumMessagePackSerializer <T>(context));
                }
#if !NET35 && !UNITY
                if (TupleItems.IsTuple(typeof(T)))
                {
                    return
                        (new ReflectionTupleMessagePackSerializer <T>(
                             context,
                             (schema ?? PolymorphismSchema.Default).ChildSchemaList
                             ));
                }
#endif // !NET35 && !UNITY

                SerializationTarget.VerifyType(typeof(T));
                var target = SerializationTarget.Prepare(context, typeof(T));
                return(new ReflectionObjectMessagePackSerializer <T>(context, target, target.GetCapabilitiesForObject()));
            }
            }
        }