Example #1
0
		/// <summary>
		///		Gets the serializer for the specified <see cref="Type"/>.
		/// </summary>
		/// <param name="targetType">Type of the serialization target.</param>
		/// <param name="providerParameter">A provider specific parameter. See remarks section of <see cref="GetSerializer{T}(Object)"/> for details.</param>
		/// <returns>
		///		<see cref="MessagePackSerializer"/>.
		///		If there is exiting one, returns it.
		///		Else the new instance will be created.
		///		If the platform supports async/await programming model, return type is <c>IAsyncMessagePackSingleObjectSerializer</c>.
		/// </returns>
		/// <exception cref="ArgumentNullException">
		///		<paramref name="targetType"/> is <c>null</c>.
		/// </exception>
		/// <remarks>
		///		Although <see cref="GetSerializer{T}(Object)"/> is preferred,
		///		this method can be used from non-generic type or methods.
		/// </remarks>
		public MessagePackSerializer GetSerializer( Type targetType, object providerParameter )
		{
			if ( targetType == null )
			{
				throw new ArgumentNullException( "targetType" );
			}

#if DEBUG
			Contract.Ensures( Contract.Result<MessagePackSerializer>() != null );
#endif // DEBUG

#if UNITY
			try
			{
#endif // UNITY
			return SerializerGetter.Instance.Get( this, targetType, providerParameter );
#if UNITY
			}
			catch ( Exception ex )
			{
				AotHelper.HandleAotError( targetType, ex );
				throw;
			}
#endif // UNITY
		}
        public object Get(SerializationContext context, Type keyType)
        {
            object matched;
            object genericDefinitionMatched;

            if (!this.Get(keyType, out matched, out genericDefinitionMatched))
            {
                return(null);
            }

            if (matched != null)
            {
                return(matched);
            }
            else
            {
#if DEBUG
                Contract.Assert(keyType.GetIsGenericType(), "keyType.GetIsGenericType()");
                Contract.Assert(!keyType.GetIsGenericTypeDefinition(), "!keyType.GetIsGenericTypeDefinition()");
#endif // DEBUG
                var type = genericDefinitionMatched as Type;
#if DEBUG
                Contract.Assert(type != null, "type != null");
                Contract.Assert(type.GetIsGenericTypeDefinition(), "type.GetIsGenericTypeDefinition()");
#endif // DEBUG
#if !UNITY
                var result =
                    ReflectionExtensions.CreateInstancePreservingExceptionType(
                        type.MakeGenericType(keyType.GetGenericArguments()),
                        context
                        );
#else
                var    resultType   = type.IsGenericTypeDefinition ? type.MakeGenericType(keyType.GetGenericArguments()) : type;
                var    constructor2 = resultType.GetConstructor(NonGenericSerializerConstructorParameterTypes);
                object result;
                try
                {
                    result =
                        constructor2 == null
                                                ? ReflectionExtensions.CreateInstancePreservingExceptionType(resultType, context)
                                                : ReflectionExtensions.CreateInstancePreservingExceptionType(resultType, context, keyType);
                }
                catch (Exception ex)
                {
                    AotHelper.HandleAotError(keyType, ex);
                    throw;
                }
#endif // !UNITY
                Contract.Assert(result != null, "result != null");

                return(result);
            }
        }