internal static MessagePackSerializer <T> CreateInternal <T>(SerializationContext context)
        {
#if !XAMIOS && !XAMDROID && !UNITY
            Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null);
#endif // !XAMIOS && !XAMDROID && !UNITY
#if XAMIOS || XAMDROID || UNITY
            return(CreateReflectionInternal <T>(context));
#else
            ISerializerBuilder <T> builder;
#if NETFX_CORE || WINDOWS_PHONE
            builder = new ExpressionTreeSerializerBuilder <T>();
#elif SILVERLIGHT
            builder = new DynamicMethodSerializerBuilder <T>();
#else
            switch (context.EmitterFlavor)
            {
            case EmitterFlavor.ReflectionBased:
            {
                return(CreateReflectionInternal <T>(context));
            }

#if !WINDOWS_PHONE && !NETFX_35
            case EmitterFlavor.ExpressionBased:
            {
                builder = new ExpressionTreeSerializerBuilder <T>();
                break;
            }
#endif // if !WINDOWS_PHONE && !NETFX_35
            case EmitterFlavor.FieldBased:
            {
                builder = new AssemblyBuilderSerializerBuilder <T>();
                break;
            }

            case EmitterFlavor.ContextBased:
            {
                builder = new DynamicMethodSerializerBuilder <T>();
                break;
            }

            default:
            {
#if !NETFX_35
                if (!SerializerDebugging.OnTheFlyCodeDomEnabled)
                {
                    throw new NotSupportedException(
                              String.Format(
                                  CultureInfo.CurrentCulture,
                                  "Flavor '{0:G}'({0:D}) is not supported for serializer instance creation.",
                                  context.EmitterFlavor
                                  )
                              );
                }
#endif // if !NETFX_35
                builder = new CodeDomSerializerBuilder <T>();
                break;
            }
            }
#endif // NETFX_CORE else

            return(new AutoMessagePackSerializer <T>(context, builder));
#endif // XAMIOS || XAMDROID || UNITY else
        }
Beispiel #2
0
        internal static MessagePackSerializer <T> CreateInternal <T>(SerializationContext context, PolymorphismSchema schema)
        {
#if DEBUG
            Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null);
#endif // DEBUG

#if DEBUG && !AOT && !SILVERLIGHT && !NETSTANDARD1_1
            SerializerDebugging.TraceEmitEvent(
                "SerializationContext::CreateInternal<{0}>(@{1}, {2})",
                typeof(T),
                context.GetHashCode(),
                schema == null ? "null" : schema.DebugString
                );
#endif // DEBUG && !AOT && !SILVERLIGHT && !NETSTANDARD1_1
            Type             concreteType     = null;
            CollectionTraits collectionTraits =
#if AOT
                typeof(T).GetCollectionTraits(CollectionTraitOptions.None, context.CompatibilityOptions.AllowNonCollectionEnumerableTypes);
#else
                typeof(T).GetCollectionTraits(CollectionTraitOptions.Full, context.CompatibilityOptions.AllowNonCollectionEnumerableTypes);
#endif // AOT

            if (typeof(T).GetIsAbstract() || typeof(T).GetIsInterface())
            {
                // Abstract collection types will be handled correctly.
                if (collectionTraits.CollectionType != CollectionKind.NotCollection)
                {
                    concreteType = context.DefaultCollectionTypes.GetConcreteType(typeof(T));
                }

                if (concreteType == null)
                {
                    // return null for polymoirphic provider.
                    return(null);
                }

                ValidateType(concreteType);
            }
            else
            {
                ValidateType(typeof(T));
            }

#if FEATURE_EMIT
            ISerializerBuilder builder;
            switch (context.SerializerOptions.EmitterFlavor)
            {
#if !NETSTANDARD1_3
            case EmitterFlavor.CodeDomBased:
            {
#if DEBUG
                if (!SerializerDebugging.OnTheFlyCodeGenerationEnabled)
                {
                    throw new NotSupportedException(
                              String.Format(
                                  CultureInfo.CurrentCulture,
                                  "Flavor '{0:G}'({0:D}) is not supported for serializer instance creation.",
                                  context.SerializerOptions.EmitterFlavor
                                  )
                              );
                }

                builder = new CodeDomSerializerBuilder(typeof(T), collectionTraits);
                break;
#else // DEBUG
                throw new NotSupportedException();
#endif // DEBUG
            }
#endif // !NETSTANDARD1_3
            case EmitterFlavor.FieldBased:
            {
                builder = new AssemblyBuilderSerializerBuilder(typeof(T), collectionTraits);
                break;
            }

            default:                     // EmitterFlavor.ReflectionBased
            {
#endif // FEATURE_EMIT
            return
                (GenericSerializer.TryCreateAbstractCollectionSerializer(context, typeof(T), concreteType, schema) as MessagePackSerializer <T>
                 ?? CreateReflectionInternal <T>(context, concreteType ?? typeof(T), schema));

#if FEATURE_EMIT
        }
    }

    return((MessagePackSerializer <T>)builder.BuildSerializerInstance(context, concreteType, schema == null ? null : schema.FilterSelf()));
#endif // FEATURE_EMIT
        }
        internal static MessagePackSerializer <T> CreateInternal <T>(SerializationContext context, PolymorphismSchema schema)
        {
#if !XAMIOS && !XAMDROID && !UNITY
            Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null);
#endif // !XAMIOS && !XAMDROID && !UNITY

#if DEBUG && !UNITY && !XAMDROID && !XAMIOS
            SerializerDebugging.TraceEvent(
                "SerializationContext::CreateInternal<{0}>(@{1}, {2})",
                typeof(T),
                context.GetHashCode(),
                schema == null ? "null" : schema.DebugString
                );
#endif // DEBUG && !UNITY && !XAMDROID && !XAMIOS
            Type concreteType = null;

            if (typeof(T).GetIsAbstract() || typeof(T).GetIsInterface())
            {
                // Abstract collection types will be handled correctly.
                if (typeof(T).GetCollectionTraits().CollectionType != CollectionKind.NotCollection)
                {
                    concreteType = context.DefaultCollectionTypes.GetConcreteType(typeof(T));
                }

                if (concreteType == null)
                {
                    // return null for polymoirphic provider.
                    return(null);
                }

                ValidateType(concreteType);
            }
            else
            {
                ValidateType(typeof(T));
            }

#if !XAMIOS && !XAMDROID && !UNITY
            ISerializerBuilder <T> builder;
#endif // !XAMIOS && !XAMDROID && !UNITY
#if NETFX_CORE || WINDOWS_PHONE
            builder = new ExpressionTreeSerializerBuilder <T>();
#elif SILVERLIGHT
            builder = new DynamicMethodSerializerBuilder <T>();
#else
#if !XAMIOS && !XAMDROID && !UNITY
            switch (context.EmitterFlavor)
            {
            case EmitterFlavor.ReflectionBased:
            {
#endif // !XAMIOS && !XAMDROID && !UNITY
            return
                (DefaultSerializers.GenericSerializer.TryCreateAbstractCollectionSerializer(context, typeof(T), concreteType, schema) as MessagePackSerializer <T>
                 ?? CreateReflectionInternal <T>(context, concreteType ?? typeof(T), schema));

#if !XAMIOS && !XAMDROID && !UNITY
        }

#if !WINDOWS_PHONE && !NETFX_35
    case EmitterFlavor.ExpressionBased:
    {
        builder = new ExpressionTreeSerializerBuilder <T>();
        break;
    }
#endif // if !WINDOWS_PHONE && !NETFX_35
    case EmitterFlavor.FieldBased:
    {
        builder = new AssemblyBuilderSerializerBuilder <T>();
        break;
    }

    case EmitterFlavor.ContextBased:
    {
        builder = new DynamicMethodSerializerBuilder <T>();
        break;
    }

    default:
    {
#if !NETFX_35
        if (!SerializerDebugging.OnTheFlyCodeDomEnabled)
        {
            throw new NotSupportedException(
                      String.Format(
                          CultureInfo.CurrentCulture,
                          "Flavor '{0:G}'({0:D}) is not supported for serializer instance creation.",
                          context.EmitterFlavor
                          )
                      );
        }
#endif // if !NETFX_35
        builder = new CodeDomSerializerBuilder <T>();
        break;
    }
    }
#endif // !XAMIOS && !XAMDROID && !UNITY
#endif // NETFX_CORE else
#if !XAMIOS && !XAMDROID && !UNITY
            return(builder.BuildSerializerInstance(context, concreteType, schema == null ? null : schema.FilterSelf()));
#endif // !XAMIOS && !XAMDROID && !UNITY
        }