Esempio n. 1
0
        /// <summary>
        /// Generate an expression that call the clone implementation method
        /// </summary>
        /// <param name="source">Source object</param>
        /// <param name="refTrackerParam">Reference tracker</param>
        /// <param name="expectedType">Expected type....mostly used for IQueryable detection</param>
        /// <returns>An expression that call the clone implementation method</returns>
        internal static Expression CallCopyExpression(Expression source, ParameterExpression refTrackerParam, Expression expectedType)
        {
            var itemAsObject = Expression.Parameter(typeof(object), "itemAsObject");
            var variables    = new List <ParameterExpression>();

            variables.Add(itemAsObject);

            var expressions = new List <Expression>();

            expressions.Add(Expression.Assign(itemAsObject, Expression.Call(SerializerMih.PrepareObjectForSerialization(), source)));
            expressions.Add(Expression.Assign(itemAsObject, Expression.Call(Expression.Call(ObjectClonerMih.CloneImpl(),
                                                                                            Expression.Call(itemAsObject, ObjectMih.GetTypeMethod())),
                                                                            FuncMih.CloneMethodInvoke(),
                                                                            Expression.Convert(itemAsObject, typeof(object)),
                                                                            refTrackerParam)));
            expressions.Add(Expression.Call(SerializerMih.ConvertObjectToExpectedType(),
                                            itemAsObject,
                                            expectedType));

            return(Expression.Block(variables, expressions));
        }
Esempio n. 2
0
        /// <summary>
        /// Generates an expression tree to handle class type serialization
        /// </summary>
        /// <param name="outputStream">Stream to write to</param>
        /// <param name="objTracking">Reference tracker</param>
        /// <param name="item">Source object</param>
        /// <param name="itemAsObj">Source object as an object</param>
        /// <param name="typeExpr">Type of the object as an expression</param>
        /// <param name="serializer">Serializer temporary variable</param>
        /// <param name="itemType">Type of the element</param>
        /// <returns>An expression tree to handle class type serialization</returns>
        internal static Expression GetWriteClassTypeExpression(ParameterExpression outputStream,
                                                               ParameterExpression objTracking,
                                                               Expression item,
                                                               Expression itemAsObj,
                                                               ParameterExpression typeExpr,
                                                               ParameterExpression serializer,
                                                               Type itemType)
        {
            var writeType = Expression.IfThenElse(Expression.Equal(Expression.Call(item, ObjectMih.GetTypeMethod()), Expression.Constant(itemType)),
                                                  Expression.Block(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value0), objTracking),
                                                                   Expression.Assign(typeExpr, Expression.Constant(itemType)),
                                                                   Expression.Assign(itemAsObj, Expression.Convert(item, typeof(object)))),
                                                  Expression.Block(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value1), objTracking),
                                                                   Expression.Assign(itemAsObj, Expression.Call(SerializerMih.PrepareObjectForSerialization(), item)),
                                                                   Expression.Assign(typeExpr, Expression.Call(itemAsObj, ObjectMih.GetTypeMethod())),
                                                                   GenerateStringExpression(outputStream, Expression.Call(SerializedTypeResolverMih.GetShortNameFromType(), typeExpr), objTracking),
                                                                   PrimitiveHelpers.WriteInt32(outputStream, Expression.Call(SerializedTypeResolverMih.GetHashCodeFromType(), typeExpr), objTracking)));

            return(Expression.IfThenElse(Expression.Equal(item, Expression.Constant(null)),
                                         PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value0), objTracking),
                                         Expression.Block(PrimitiveHelpers.WriteByte(outputStream, Expression.Constant(SerializerObjectTracker.Value1), objTracking),
                                                          writeType,
                                                          Expression.Assign(serializer, Expression.Call(SerializerMih.GetTypeToObjectSerializer(), typeExpr)),
                                                          Expression.Invoke(serializer, outputStream, itemAsObj, objTracking))));
        }
Esempio n. 3
0
        internal static void GenerateCopyFieldsExpressions(IEnumerable <FieldInfo> fields,
                                                           ParameterExpression source,
                                                           ParameterExpression clone,
                                                           List <Expression> expressions,
                                                           ParameterExpression refTrackerParam,
                                                           ParameterExpression clonedItem)
        {
            foreach (var field in fields)
            {
                var cloneField  = Expression.Field(clone, field);
                var sourceField = Expression.Field(source, field);
                if (field.FieldType.IsPrimitive || field.FieldType.IsValueType || (field.FieldType == typeof(string)))
                {
                    if (field.IsInitOnly)
                    {
                        expressions.Add(Expression.Call(CopyReadOnlyFieldMethodInfo.GetMethodInfo(),
                                                        Expression.Constant(field),
                                                        Expression.Convert(sourceField, typeof(object)),
                                                        clone));
                    }
                    else
                    {
                        var from = sourceField;
                        var to   = cloneField;
                        expressions.Add(Expression.Assign(to, from));
                    }
                }
                else
                {
                    var es = new List <Expression>();
                    es.Add(Expression.Assign(clonedItem, Expression.Call(SerializerMih.PrepareObjectForSerialization(), sourceField)));
                    es.Add(Expression.Assign(clonedItem, CallCopyExpression(clonedItem, refTrackerParam, Expression.Constant(field.FieldType))));

                    if (IQueryableCloner.IsGenericIQueryableType(field.FieldType))
                    {
                        Type queryableInterface  = IQueryableCloner.GetInterfaceType(field.FieldType, typeof(IQueryable <>));
                        var  genericArgumentType = queryableInterface.GetGenericArguments()[0];

                        var copy2 = Expression.Convert(clonedItem, field.FieldType);
                        var m     = Expression.Call(typeof(Queryable), "AsQueryable", new Type[] { genericArgumentType }, Expression.Convert(copy2, typeof(IEnumerable <>).MakeGenericType(genericArgumentType)));
                        es.Add(Expression.Assign(clonedItem, m));
                    }

                    if (field.FieldType == typeof(IQueryable))
                    {
                        es.Add(Expression.Assign(clonedItem, Expression.Call(IQueryableMih.ConvertToNonGenericQueryable(), clonedItem)));
                    }

                    if (field.IsInitOnly)
                    {
                        Func <Expression, Expression> assignExpr = exx => Expression.Call(CopyReadOnlyFieldMethodInfo.GetMethodInfo(),
                                                                                          Expression.Constant(field),
                                                                                          Expression.Convert(exx, typeof(object)),
                                                                                          clone);

                        var assignNullExpr = Expression.Call(CopyReadOnlyFieldMethodInfo.GetMethodInfo(),
                                                             Expression.Constant(field),
                                                             Expression.Constant(null, typeof(object)),
                                                             clone);

                        es.Add(clonedItem);
                        var conditionalExpression = Expression.IfThenElse(Expression.NotEqual(sourceField, Expression.Constant(null)),
                                                                          assignExpr(Expression.Block(es)),
                                                                          assignNullExpr);

                        expressions.Add(ObjectCloner.GenerateNullTrackedOrUntrackedExpression(sourceField,
                                                                                              cloneField,
                                                                                              field.FieldType,
                                                                                              refTrackerParam,
                                                                                              conditionalExpression,
                                                                                              trackedExpression: assignExpr)); // todo refactor this horrible method
                    }
                    else
                    {
                        es.Add(Expression.Assign(cloneField, Expression.Convert(clonedItem, field.FieldType)));


                        var conditionalExpression = Expression.IfThenElse(Expression.Equal(sourceField, Expression.Constant(null)),
                                                                          Expression.Assign(cloneField, Expression.Constant(null, field.FieldType)),
                                                                          Expression.Block(es));

                        expressions.Add(ObjectCloner.GenerateNullTrackedOrUntrackedExpression(sourceField,
                                                                                              cloneField,
                                                                                              field.FieldType,
                                                                                              refTrackerParam,
                                                                                              conditionalExpression));
                    }
                }
            }
        }