internal static void GetNullableCompanion(
            Type targetType,
            SerializationContext context,
            object serializer,
            out Type nullableType,
            out MessagePackSerializerProvider nullableSerializerProvider
            )
        {
            if (targetType.GetIsValueType() && Nullable.GetUnderlyingType(targetType) == null)
            {
                nullableType = typeof(Nullable <>).MakeGenericType(targetType);
                var nullableCtor =
                    typeof(NullableMessagePackSerializer <>).MakeGenericType(targetType).GetConstructor(
                        new[]
                {
                    typeof(SerializationContext),
                    typeof(MessagePackSerializer <>).MakeGenericType(targetType)
                }
                        );

                nullableSerializerProvider =
                    ( MessagePackSerializerProvider )ReflectionExtensions.CreateInstancePreservingExceptionType(
                        typeof(PolymorphicSerializerProvider <>).MakeGenericType(nullableType),
                        nullableCtor.InvokePreservingExceptionType(
                            context,
                            serializer
                            )
                        );
            }
            else
            {
                nullableType = null;
                nullableSerializerProvider = null;
            }
        }
Exemple #2
0
        internal bool Register(Type targetType, MessagePackSerializerProvider serializer)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            return(this._repository.Register(targetType, serializer, /*allowOverwrite*/ false));
        }
        /// <summary>
        ///		Registers a <see cref="MessagePackSerializer{T}"/>.
        /// </summary>
        /// <typeparam name="T">The type of serialization target.</typeparam>
        /// <param name="serializer"><see cref="MessagePackSerializer{T}"/> instance.</param>
        /// <param name="options">A <see cref="SerializerRegistrationOptions"/> to control this registration process.</param>
        /// <returns>
        ///		<c>true</c> if success to register; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="serializer"/> is <c>null</c>.
        /// </exception>
        public bool Register <T>(MessagePackSerializer <T> serializer, SerializerRegistrationOptions options)
        {
            if (serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }

            Type nullableType = null;
            MessagePackSerializerProvider nullableSerializerProvider = null;

#if !UNITY
            if ((options & SerializerRegistrationOptions.WithNullable) != 0)
            {
                GetNullableCompanion(typeof(T), serializer.OwnerContext, serializer, out nullableType, out nullableSerializerProvider);
            }
#endif // !UNITY
#if !UNITY
            return(this.Register(typeof(T), new PolymorphicSerializerProvider <T>(serializer), nullableType, nullableSerializerProvider, options));
#else
            return(this.Register(typeof(T), new PolymorphicSerializerProvider <T>(serializer.OwnerContext, serializer), nullableType, nullableSerializerProvider, options));
#endif // !UNITY
        }
 internal bool Register(Type targetType, MessagePackSerializerProvider serializerProvider, Type nullableType, MessagePackSerializerProvider nullableSerializerProvider, SerializerRegistrationOptions options)
 {
     return(this._repository.Register(targetType, serializerProvider, nullableType, nullableSerializerProvider, options));
 }