示例#1
0
        public static SerializerEmitter CreateTupleSerializerCore(Type tupleType, EmitterFlavor emitterFlavor)
        {
            Contract.Requires(tupleType != null);
            Contract.Ensures(Contract.Result <SerializerEmitter>() != null);

            var emitter   = SerializationMethodGeneratorManager.Get().CreateEmitter(tupleType, emitterFlavor);
            var itemTypes = TupleItems.GetTupleItemTypes(tupleType);

            CreateTuplePack(
                emitter,
                tupleType,
                itemTypes,
                (il, collection) =>
            {
                il.EmitAnyLdarg(2);
                il.EmitAnyStloc(collection);
            }
                );
            CreateTupleUnpack(
                emitter,
                itemTypes
                );

            return(emitter);
        }
        private void BuildTupleSerializer(TContext context)
        {
            var itemTypes = TupleItems.GetTupleItemTypes(typeof(TObject));

            this.BuildTuplePackTo(context, itemTypes);
            this.BuildTupleUnpackFrom(context, itemTypes);
        }
        private void BuildTupleSerializer(TContext context, IList <PolymorphismSchema> itemSchemaList, out SerializationTarget targetInfo)
        {
            var itemTypes = TupleItems.GetTupleItemTypes(this.TargetType);

            targetInfo = SerializationTarget.CreateForTuple(itemTypes);
            var isValueTuple = this.TargetType.GetIsValueType();

            this.BuildTuplePackTo(context, itemTypes, itemSchemaList, isValueTuple, false);

#if FEATURE_TAP
            if (this.WithAsync(context))
            {
                this.BuildTuplePackTo(context, itemTypes, itemSchemaList, isValueTuple, true);
            }
#endif // FEATURE_TAP

            this.BuildTupleUnpackFrom(context, itemTypes, itemSchemaList, isValueTuple, false);

#if FEATURE_TAP
            if (this.WithAsync(context))
            {
                this.BuildTupleUnpackFrom(context, itemTypes, itemSchemaList, isValueTuple, true);
            }
#endif // FEATURE_TAP
        }
示例#4
0
        private void BuildTupleSerializer(TContext context, IList <PolymorphismSchema> itemSchemaList)
        {
            var itemTypes = TupleItems.GetTupleItemTypes(typeof(TObject));

            this.BuildTuplePackTo(context, itemTypes, itemSchemaList);
            this.BuildTupleUnpackFrom(context, itemTypes, itemSchemaList);
        }
        public ReflectionTupleMessagePackSerializer(SerializationContext ownerContext)
            : base(ownerContext)
        {
            var itemTypes = TupleItems.GetTupleItemTypes(typeof(T));

            this._itemSerializers   = itemTypes.Select(itemType => ownerContext.GetSerializer(itemType)).ToArray();
            this._tupleTypes        = TupleItems.CreateTupleTypeList(itemTypes);
            this._tupleConstructors = this._tupleTypes.Select(tupleType => tupleType.GetConstructors().Single()).ToArray();
            this._getters           = GetGetters(itemTypes, this._tupleTypes).ToArray();
        }
        private static void VerifyArity(Type tupleType, ICollection <PolymorphismSchema> itemSchemaList)
        {
            if (itemSchemaList == null || itemSchemaList.Count == 0)
            {
                // OK
                return;
            }

            if (TupleItems.GetTupleItemTypes(tupleType).Count != itemSchemaList.Count)
            {
                throw new ArgumentException("An arity of itemSchemaList does not match for an arity of the tuple.", "itemSchemaList");
            }
        }
示例#7
0
        public ReflectionTupleMessagePackSerializer(SerializationContext ownerContext, IList <PolymorphismSchema> itemSchemas)
            : base(ownerContext)
        {
            var itemTypes = TupleItems.GetTupleItemTypes(typeof(T));

            this._itemSerializers =
                itemTypes.Select(
                    (itemType, i) => ownerContext.GetSerializer(itemType, itemSchemas.Count == 0 ? null : itemSchemas[i])
                    ).ToArray();
            this._tupleTypes        = TupleItems.CreateTupleTypeList(itemTypes);
            this._tupleConstructors = this._tupleTypes.Select(tupleType => tupleType.GetConstructors().Single()).ToArray();
            this._getters           = GetGetters(itemTypes, this._tupleTypes).ToArray();
        }
        public ReflectionTupleMessagePackSerializer(SerializationContext ownerContext, IList <PolymorphismSchema> itemSchemas)
            : base(ownerContext, SerializerCapabilities.PackTo | SerializerCapabilities.UnpackFrom)
        {
            var itemTypes = TupleItems.GetTupleItemTypes(typeof(T));

            this._itemSerializers =
                itemTypes.Select(
                    (itemType, i) => ownerContext.GetSerializer(itemType, itemSchemas.Count == 0 ? null : itemSchemas[i])
                    ).ToArray();
            this._tupleTypes        = TupleItems.CreateTupleTypeList(typeof(T));
            this._tupleConstructors = this._tupleTypes.Select(tupleType => tupleType.GetConstructors().SingleOrDefault()).ToArray();
            this._getters           =
                typeof(T).GetIsValueType()
                                        ? GetGetters(
                    itemTypes,
                    this._tupleTypes,
                    (type, name) => type.GetField(name),
                    f => f,
                    getters =>
                    tuple =>
            {
                object current = tuple;
                foreach (var getter in getters)
                {
                    current = getter.GetValue(current);
                }

                return(current);
            }
                    ).ToArray()
                                        : GetGetters(
                    itemTypes,
                    this._tupleTypes,
                    (type, name) => type.GetProperty(name),
                    p => p.GetGetMethod(),
                    getters =>
                    tuple =>
            {
                object current = tuple;
                foreach (var getter in getters)
                {
                    current = getter.InvokePreservingExceptionType(current);
                }

                return(current);
            }
                    ).ToArray();
        }
        public TupleExpressionMessagePackSerializer(SerializationContext context)
            : base((context ?? SerializationContext.Default).CompatibilityOptions.PackerCompatibilityOptions)
        {
            Contract.Assert(typeof(T).FullName.StartsWith("System.Tuple`", StringComparison.Ordinal), typeof(T) + " is not Tuple<...>");
            var flattenTypes = TupleItems.GetTupleItemTypes(typeof(T));

            this._itemSerializers = flattenTypes.Select(t => context.GetSerializer(t)).ToArray();

            var packerParameter          = Expression.Parameter(typeof(Packer), "packer");
            var objectTreeParameter      = Expression.Parameter(typeof(T), "objectTree");
            var itemSerializersParameter = Expression.Parameter(typeof(IMessagePackSerializer[]), "itemSerializers");
            var itemSerializerTypes      = flattenTypes.Select(t => typeof(MessagePackSerializer <>).MakeGenericType(t)).ToArray();

            /*
             *	packer.PackArrayHeader( arity );
             *	serializer[ 0 ].PackTo( pack, tuple.Item1 );
             *		:
             *	serializer[ N ].PackTo( pack, tuple.Rest....ItemM );
             */
            this._packToCore =
                Expression.Lambda <Action <Packer, T, IMessagePackSerializer[]> >(
                    Expression.Block(
                        new[] {
                Expression.Call(
                    packerParameter,
                    Metadata._Packer.PackArrayHeader,
                    Expression.Constant(flattenTypes.Count)
                    )
            }.Concat(
                            CreatePackExpressions(packerParameter, objectTreeParameter, itemSerializersParameter, itemSerializerTypes, flattenTypes)
                            ).ToArray()
                        ),
                    packerParameter, objectTreeParameter, itemSerializersParameter
                    ).Compile();

            var unpackerParameter = Expression.Parameter(typeof(Unpacker), "unpacker");

            /*
             *  checked
             *  {
             *		if (!unpacker.Read())
             *		{
             *			throw SerializationExceptions.NewMissingItem(0);
             *		}
             *
             *		T1 item1;
             *		if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
             *		{
             *			item1 = this._serializer0.UnpackFrom(unpacker);
             *		}
             *		else
             *		{
             *			using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
             *			{
             *				item1 = this._serializer0.UnpackFrom(subtreeUnpacker);
             *			}
             *		}
             *
             *		if (!unpacker.Read())
             *			:
             *
             *		return new Tuple<...>( item1, ... , new Tuple<...>(...)...);
             *	}
             */

            this._unpackFromCore =
                Expression.Lambda <Func <Unpacker, IMessagePackSerializer[], T> >(
                    CreateNestedTupleCreationExpression(
                        flattenTypes,
                        CreateUnpackExpressions(unpackerParameter, itemSerializersParameter, itemSerializerTypes, flattenTypes).ToArray()
                        ),
                    unpackerParameter, itemSerializersParameter
                    ).Compile();
        }