예제 #1
0
        public static IMessagePackSingleObjectSerializer CreateCollectionSerializer <T>(
#endif // !UNITY
            SerializationContext context,
            Type targetType,
            CollectionTraits traits,
            PolymorphismSchema schema
            )
        {
            switch (traits.DetailedCollectionType)
            {
            case CollectionDetailedKind.Array:
            {
                return(ArraySerializer.Create <T>(context, schema));
            }

            case CollectionDetailedKind.GenericList:
#if !NETFX_35 && !UNITY
            case CollectionDetailedKind.GenericSet:
#endif // !NETFX_35 && !UNITY
            case CollectionDetailedKind.GenericCollection:
            {
                return
                    (#if !UNITY
                     (MessagePackSerializer <T>)
                     ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>(
                         typeof(CollectionSerializerFactory <,>).MakeGenericType(typeof(T), traits.ElementType)
                         ).Create(context, targetType, schema));
#else
                     new ReflectionCollectionMessagePackSerializer(context, typeof(T), targetType, traits, schema);
#endif // !UNITY
            }

            case CollectionDetailedKind.GenericEnumerable:
            {
                return
                    (#if !UNITY
                     (MessagePackSerializer <T>)
                     ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>(
                         typeof(EnumerableSerializerFactory <,>).MakeGenericType(typeof(T), traits.ElementType)
                         ).Create(context, targetType, schema));
#else
                     new ReflectionEnumerableMessagePackSerializer(context, typeof(T), targetType, traits, schema);
#endif // !Enumerable
            }

            case CollectionDetailedKind.GenericDictionary:
            {
                var genericArgumentOfKeyValuePair = traits.ElementType.GetGenericArguments();
                return
                    (#if !UNITY
                     (MessagePackSerializer <T>)
                     ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>(
                         typeof(DictionarySerializerFactory <, ,>).MakeGenericType(
                             typeof(T),
                             genericArgumentOfKeyValuePair[0],
                             genericArgumentOfKeyValuePair[1]
                             )
                         ).Create(context, targetType, schema));
#else
                     new ReflectionDictionaryMessagePackSerializer(
                         context,
                         typeof(T),
                         targetType,
                         genericArgumentOfKeyValuePair[0],
                         genericArgumentOfKeyValuePair[1],
                         traits,
                         schema
                         );
#endif // !UNITY
            }

            case CollectionDetailedKind.NonGenericList:
            {
                return
                    (#if !UNITY
                     (MessagePackSerializer <T>)
                     ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>(
                         typeof(NonGenericListSerializerFactory <>).MakeGenericType(typeof(T))
                         ).Create(context, targetType, schema));
#else
                     new ReflectionNonGenericListMessagePackSerializer(context, typeof(T), targetType, schema);
#endif // !UNITY
            }

            case CollectionDetailedKind.NonGenericCollection:
            {
                return
                    (#if !UNITY
                     (MessagePackSerializer <T>)
                     ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>(
                         typeof(NonGenericCollectionSerializerFactory <>).MakeGenericType(typeof(T), traits.ElementType)
                         ).Create(context, targetType, schema));
#else
                     new ReflectionNonGenericCollectionMessagePackSerializer(context, typeof(T), targetType, schema);
#endif // !UNITY
            }

            case CollectionDetailedKind.NonGenericEnumerable:
            {
                return
                    (#if !UNITY
                     (MessagePackSerializer <T>)
                     ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>(
                         typeof(NonGenericEnumerableSerializerFactory <>).MakeGenericType(typeof(T), traits.ElementType)
                         ).Create(context, targetType, schema));
#else
                     new ReflectionNonGenericEnumerableMessagePackSerializer(context, typeof(T), targetType, schema);
#endif // !UNITY
            }

            case CollectionDetailedKind.NonGenericDictionary:
            {
                return
                    (#if !UNITY
                     (MessagePackSerializer <T>)
                     ReflectionExtensions.CreateInstancePreservingExceptionType <IVariantReflectionSerializerFactory>(
                         typeof(NonGenericDictionarySerializerFactory <>).MakeGenericType(typeof(T))
                         ).Create(context, targetType, schema));
#else
                     new ReflectionNonGenericDictionaryMessagePackSerializer(context, typeof(T), targetType, schema);
#endif // !UNITY
            }

            default:
            {
                return(null);
            }
            }
        }
예제 #2
0
        public static MessagePackSerializer <T> CreateArraySerializer <T>(
            SerializationContext context,
            Type targetType,
            CollectionTraits traits)
        {
            switch (traits.DetailedCollectionType)
            {
            case CollectionDetailedKind.Array:
            {
                return(ArraySerializer.Create <T>(context));
            }

            case CollectionDetailedKind.GenericList:
            {
                return
                    (new ReflectionCollectionSerializer <T>(
                         context,
                         Activator.CreateInstance(
                             typeof(ListSerializer <>).MakeGenericType(traits.ElementType),
                             context,
                             targetType
                             ) as IMessagePackSerializer
                         ));
            }

#if !NETFX_35 && !UNITY
            case CollectionDetailedKind.GenericSet:
            {
                return
                    (new ReflectionCollectionSerializer <T>(
                         context,
                         Activator.CreateInstance(
                             typeof(SetSerializer <>).MakeGenericType(traits.ElementType),
                             context,
                             targetType
                             ) as IMessagePackSerializer
                         ));
            }
#endif // !NETFX_35 && !UNITY
            case CollectionDetailedKind.GenericCollection:
            {
                return
                    (new ReflectionCollectionSerializer <T>(
                         context,
                         Activator.CreateInstance(
                             typeof(CollectionSerializer <>).MakeGenericType(traits.ElementType),
                             context,
                             targetType
                             ) as IMessagePackSerializer
                         ));
            }

            case CollectionDetailedKind.GenericEnumerable:
            {
                return
                    (new ReflectionCollectionSerializer <T>(
                         context,
                         Activator.CreateInstance(
                             typeof(EnumerableSerializer <>).MakeGenericType(traits.ElementType),
                             context,
                             targetType
                             ) as IMessagePackSerializer
                         ));
            }

            case CollectionDetailedKind.NonGenericList:
            {
                return
                    (new ReflectionCollectionSerializer <T>(
                         context,
                         new NonGenericListSerializer(context, targetType)
                         ));
            }

            case CollectionDetailedKind.NonGenericCollection:
            {
                return
                    (new ReflectionCollectionSerializer <T>(
                         context,
                         new NonGenericCollectionSerializer(context, targetType)
                         ));
            }

            default:
            {
#if DEBUG && !UNITY
                Contract.Assert(traits.DetailedCollectionType == CollectionDetailedKind.NonGenericEnumerable);
#endif // DEBUG && !UNITY
                return
                    (new ReflectionCollectionSerializer <T>(
                         context,
                         new NonGenericEnumerableSerializer(context, targetType)
                         ));
            }
            }
        }