/// <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
        }
Beispiel #2
0
        private bool RegisterCore(Type key, object value, Type nullableType, object nullableValue, SerializerRegistrationOptions options)
        {
            var allowOverwrite = (options & SerializerRegistrationOptions.AllowOverride) != 0;

            if (allowOverwrite || !this.ContainsType(key, nullableType))
            {
                bool holdsWriteLock = false;
#if !SILVERLIGHT && !NETFX_CORE
                RuntimeHelpers.PrepareConstrainedRegions();
#endif
                try
                {
#if !SILVERLIGHT && !NETFX_CORE
                    RuntimeHelpers.PrepareConstrainedRegions();
#endif
                    try { }
                    finally
                    {
                        this._lock.EnterWriteLock();
                        holdsWriteLock = true;
                    }
                    if (allowOverwrite || !this.ContainsType(key, nullableType))
                    {
                        this._table[key.TypeHandle] = value;
                        if (nullableValue != null)
                        {
                            this._table[nullableType.TypeHandle] = nullableValue;
                        }

                        return(true);
                    }
                }
                finally
                {
                    if (holdsWriteLock)
                    {
                        this._lock.ExitWriteLock();
                    }
                }
            }

            return(false);
        }
Beispiel #3
0
        public bool Register(Type type, object entry, Type nullableType, object nullableValue, SerializerRegistrationOptions options)
        {
#if !UNITY && DEBUG
            Contract.Assert(entry != null, "entry != null");
#endif // !UNITY && DEBUG

            return(this.RegisterCore(type, entry, nullableType, nullableValue, options));
        }
Beispiel #4
0
		private bool RegisterCore( Type key, object value, Type nullableType, object nullableValue, SerializerRegistrationOptions options )
		{
			var allowOverwrite = ( options & SerializerRegistrationOptions.AllowOverride ) != 0;

			if ( allowOverwrite || !this.ContainsType( key, nullableType ) )
			{
				bool holdsWriteLock = false;
#if !SILVERLIGHT && !NETFX_CORE
				RuntimeHelpers.PrepareConstrainedRegions();
#endif
				try
				{
#if !SILVERLIGHT && !NETFX_CORE
					RuntimeHelpers.PrepareConstrainedRegions();
#endif
					try { }
					finally
					{
						this._lock.EnterWriteLock();
						holdsWriteLock = true;
					}
					if ( allowOverwrite || !this.ContainsType( key, nullableType ) )
					{
						this._table[ key.TypeHandle ] = value;
						if ( nullableValue != null )
						{
							this._table[ nullableType.TypeHandle ] = nullableValue;
						}

						return true;
					}
				}
				finally
				{
					if ( holdsWriteLock )
					{
						this._lock.ExitWriteLock();
					}
				}
			}

			return false;
		}
Beispiel #5
0
		public bool Register( Type type, object entry, Type nullableType, object nullableValue, SerializerRegistrationOptions options )
		{
#if !UNITY && DEBUG
			Contract.Assert( entry != null, "entry != null" );
#endif // !UNITY && DEBUG

			return this.RegisterCore( type, entry, nullableType, nullableValue, options );
		}
 internal bool Register(Type targetType, MessagePackSerializerProvider serializerProvider, Type nullableType, MessagePackSerializerProvider nullableSerializerProvider, SerializerRegistrationOptions options)
 {
     return(this._repository.Register(targetType, serializerProvider, nullableType, nullableSerializerProvider, options));
 }