コード例 #1
0
        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
        }
コード例 #2
0
        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
        }