Esempio n. 1
0
        public static Action <object, object> CreateFieldCloner(FieldInfo field)
        {
            if (field.ReflectedType.IsValueType)
            {
                return(new Action <object, object>((source, target) =>
                {
                    var value = CreateFieldGetter(field)(source);
                    CreateFieldSetter(field)(target, value);
                }));
            }
            else
            {
                var expSource = Expression.Parameter(typeof(object), "source");
                var expTarget = Expression.Parameter(typeof(object), "target");

                var expFieldLeft  = Expression.Field(Expression.Convert(expTarget, field.ReflectedType), field);
                var expFieldRight = Expression.Field(Expression.Convert(expSource, field.ReflectedType), field);

                var main = Expression.Assign(expFieldLeft, expFieldRight);
                return((Action <object, object>)Expression.Lambda(typeof(Action <object, object>), main, expSource, expTarget).Compile());
            }
        }
Esempio n. 2
0
        public static Action <object, object> CreateFieldCloner(FieldInfo field)
        {
            ParameterExpression     expression;
            ParameterExpression     expression2;
            Action <object, object> action2 = null;

            if (field.ReflectedType.IsValueType)
            {
                if (action2 == null)
                {
                    action2 = delegate(object source, object target) {
                        object obj2 = CreateFieldGetter(field)(source);
                        CreateFieldSetter(field)(target, obj2);
                    };
                }
                return(action2);
            }
            MemberExpression left  = Expression.Field(Expression.Convert(expression2 = Expression.Parameter(typeof(object), "target"), field.ReflectedType), field);
            MemberExpression right = Expression.Field(Expression.Convert(expression = Expression.Parameter(typeof(object), "source"), field.ReflectedType), field);
            BinaryExpression body  = Expression.Assign(left, right);

            return((Action <object, object>)Expression.Lambda(typeof(Action <object, object>), body, new ParameterExpression[] { expression, expression2 }).Compile());
        }