Beispiel #1
0
 private static void PrepareTypeCore <T>(SerializationContext dummyContext)
 {
     // Ensure GetSerializer<T>( object ) is AOT-ed.
     dummyContext.GetSerializer <T>();
     // Ensure Dictionary<T, ?> is work.
     AotHelper.PrepareEqualityComparer <T>();
 }
Beispiel #2
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
		}
Beispiel #3
0
        public static IEqualityComparer <T> GetEqualityComparer <T>()
        {
#if !UNITY
            return(EqualityComparer <T> .Default);
#else
            // AotHelper is internal because it should not be API -- it is subject to change when the Unity's Mono is updated or IL2CPP becomes stable.
            return(AotHelper.GetEqualityComparer <T>());
#endif // !UNITY
        }
        /// <summary>
        ///		Try to prepare specified type for some AOT(Ahead-Of-Time) compilation environment.
        /// </summary>
        /// <typeparam name="T">The type to be prepared. Normally, this should be value type.</typeparam>
        /// <remarks>
        ///		<para>
        ///			Currently, this method only works in Unity3D build.
        ///			This method does not any work for other environments(and should be removed on JIT/AOT), but exists to simplify the application compilation.
        ///			It is recommended to use this method on start up code to reduce probability of some AOT errors.
        ///		</para>
        ///		<para>
        ///			Please note that this method do not ensure for full linkage for AOT.
        ///			Manifest or attribute based linker options (e.g. for .NET Native or Xamarin.iOS) are still required.
        ///		</para>
        /// </remarks>
        public static void PrepareType <T>()
        {
#if UNITY
            // Ensure GetSerializer<T>( object ) is AOT-ed.
            SerializationContext.Default.GetSerializer <T>(null);
            // Ensure Dictionary<T, ?> is work.
            AotHelper.PrepareEqualityComparer <T>();
#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);
            }
        }
Beispiel #6
0
 internal static object GetEqualityComparer(Type comparerType)
 {
     return(AotHelper.GetEqualityComparer(comparerType));
 }