public Expression Build(
            CopyPolicy copyPolicy,
            Type type,
            Expression source,
            Expression destination,
            MemberInfo member,
            Expression cache)
        {
            var elementType = type.GetElementType();

            if (copyPolicy == CopyPolicy.DeepCopy)
            {
                if (type.GetArrayRank() > 1)
                {
                    return(CreateDeepCopyRectangulerArrayExpression(
                               type, elementType, source, MemberAccessorGenerator.CreateGetter(destination, member), cache));
                }

                //return MemberAccessorGenerator.CreateSetter(
                //    destination,
                //    member,
                //    ClonerCache.Instance.Get(elementType, source));


                var length      = Expression.ArrayLength(source);
                var arrayAssign = MemberAccessorGenerator.CreateSetter(
                    destination,
                    member,
                    Expression.NewArrayBounds(elementType, length));

                return(CreateDeepCopyArrayExpression(
                           elementType,
                           source,
                           MemberAccessorGenerator.CreateGetter(destination, member),
                           length,
                           arrayAssign,
                           cache));
            }

            if (copyPolicy == CopyPolicy.ShallowCopy)
            {
                return(CreateShallowCopyArrayExpression(
                           type,
                           source,
                           MemberAccessorGenerator.CreateGetter(destination, member)));
            }

            throw new InvalidOperationException();
        }
示例#2
0
        public Expression Build(
            Type type,
            Expression source,
            Expression destination,
            MemberInfo member,
            Expression cache)
        {
            var cloneExpression = ClonerCache.Instance.Get(type, source, cache);

            if (type != typeof(object))
            {
                return(MemberAccessorGenerator.CreateSetter(
                           destination, member, cloneExpression));
            }

            return(MemberAccessorGenerator.CreateSetter(
                       destination,
                       member,
                       Expression.Condition(
                           //Expression.TypeEqual(source, typeof(object)),
                           ExpressionUtils.IsObjectOrValueType(source),
                           ExpressionUtils.MemberwiseClone(type, source),
                           cloneExpression)));
        }