Esempio n. 1
0
        protected override Func <SerializationContext, MessagePackSerializer <TObject> > CreateSerializerConstructor(ExpressionTreeContext codeGenerationContext)
        {
#if !NETFX_CORE && !SILVERLIGHT
            if (SerializerDebugging.DumpEnabled)
            {
                this._typeBuilder.CreateType();
            }
#endif

            // Get at this point to prevent unexpected context change.
            var packToCore     = codeGenerationContext.GetPackToCore <TObject>();
            var unpackFromCore = codeGenerationContext.GetUnpackFromCore <TObject>();
            var unpackToCore   = codeGenerationContext.GetUnpackToCore <TObject>();
            return
                (context =>
                 new ExpressionCallbackMessagePackSerializer <TObject>(
                     context,
                     packToCore,
                     unpackFromCore,
                     unpackToCore
                     ));
        }
        public static Func <SerializationContext, MessagePackSerializer <TObject> > CreateFactory <TObject>(ExpressionTreeContext codeGenerationContext, CollectionTraits traits, PolymorphismSchema schema)
        {
            // Get at this point to prevent unexpected context change.
            var packToCore     = codeGenerationContext.GetPackToCore();
            var unpackFromCore = codeGenerationContext.GetUnpackFromCore();
            var unpackToCore   = codeGenerationContext.GetUnpackToCore();
            var createInstance = codeGenerationContext.GetCreateInstance();
            var addItem        = codeGenerationContext.GetAddItem();

            switch (traits.DetailedCollectionType)
            {
            case CollectionDetailedKind.NonGenericEnumerable:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>(
                        typeof(NonGenericEnumerableCallbackSerializerFactory <>).MakeGenericType(typeof(TObject))
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, unpackFromCore, addItem) as MessagePackSerializer <TObject>);
            }

            case CollectionDetailedKind.NonGenericCollection:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>(
                        typeof(NonGenericCollectionCallbackSerializerFactory <>).MakeGenericType(typeof(TObject))
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, unpackFromCore, addItem) as MessagePackSerializer <TObject>);
            }

            case CollectionDetailedKind.NonGenericList:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>(
                        typeof(NonGenericListCallbackSerializerFactory <>).MakeGenericType(typeof(TObject))
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>);
            }

            case CollectionDetailedKind.NonGenericDictionary:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>(
                        typeof(NonGenericDictionaryCallbackSerializerFactory <>).MakeGenericType(typeof(TObject))
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>);
            }

            case CollectionDetailedKind.GenericEnumerable:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <IEnumerableCallbackSerializerFactory>(
                        typeof(EnumerableCallbackSerializerFactory <,>).MakeGenericType(typeof(TObject), traits.ElementType)
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, unpackFromCore, addItem) as MessagePackSerializer <TObject>);
            }

            case CollectionDetailedKind.GenericCollection:
            case CollectionDetailedKind.GenericSet:
            case CollectionDetailedKind.GenericList:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>(
                        typeof(CollectionCallbackSerializerFactory <,>).MakeGenericType(typeof(TObject), traits.ElementType)
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>);
            }

#if !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE)
            case CollectionDetailedKind.GenericReadOnlyCollection:
            case CollectionDetailedKind.GenericReadOnlyList:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>(
                        typeof(ReadOnlyCollectionCallbackSerializerFactory <,>).MakeGenericType(typeof(TObject), traits.ElementType)
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>);
            }
#endif // !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE )
            case CollectionDetailedKind.GenericDictionary:
            {
                var keyValuePairGenericArguments = traits.ElementType.GetGenericArguments();
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>(
                        typeof(DictionaryCallbackSerializerFactory <, ,>).MakeGenericType(
                            typeof(TObject),
                            keyValuePairGenericArguments[0],
                            keyValuePairGenericArguments[1]
                            )
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>);
            }

#if !NETFX_40 && !(SILVERLIGHT && !WINDOWS_PHONE)
            case CollectionDetailedKind.GenericReadOnlyDictionary:
            {
                var keyValuePairGenericArguments = traits.ElementType.GetGenericArguments();
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <ICollectionCallbackSerializerFactory>(
                        typeof(ReadOnlyDictionaryCallbackSerializerFactory <, ,>).MakeGenericType(
                            typeof(TObject),
                            keyValuePairGenericArguments[0],
                            keyValuePairGenericArguments[1]
                            )
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG

                return
                    (context =>
                     factory.Create(context, schema, createInstance, addItem) as MessagePackSerializer <TObject>);
            }
#endif // !NETFX_40 && !( SILVERLIGHT && !WINDOWS_PHONE )
            default:
            {
                var factory =
                    ReflectionExtensions.CreateInstancePreservingExceptionType <ICallbackSerializerFactory>(
                        typeof(CallbackSerializerFactory <>).MakeGenericType(
                            typeof(TObject)
                            )
                        );
#if DEBUG
                Contract.Assert(factory != null);
#endif // DEBUG
                return
                    (context =>
                     factory.Create(context, packToCore, unpackFromCore, unpackToCore) as MessagePackSerializer <TObject>);
            }
            }
        }