Beispiel #1
0
 public static Func <T> CreateGetter <T>(Expression <Func <T> > memberAccess)
 {
     return(() => (T)CreateGetter(memberAccess.Body)());
 }
Beispiel #2
0
        private T CloneWithMembers <T>(T source, IReadOnlyCollection <Tuple <MemberInfo, MemberTypes, CloningMode> > members)
        {
            var copy = CreateInstance(source);

            _cloned.Add(source, copy);

            foreach (var member in members)
            {
                if (member.Item3 == CloningMode.Ignore)
                {
                    continue;
                }

                Func <object>           valueGetter;
                Action <object, object> valueSetter;
                Func <object>           structValueGetter;
                Action <object, object> structValueSetter;
                switch (member.Item2)
                {
                case MemberTypes.Property:
                    var property = member.Item1 as PropertyInfo;
                    valueGetter       = () => CreatePropGetter <T, object>(property)(source);
                    valueSetter       = (target, value) => CreatePropSetter <T, object>(property)((T)target, value);
                    structValueGetter = () => property.GetValue(source);
                    structValueSetter = (target, value) => property.SetValue(target, value);
                    break;

                case MemberTypes.Field:
                    var field = member.Item1 as FieldInfo;
                    valueGetter       = () => CreateGetter <T, object>(field)(source);
                    valueSetter       = (target, value) => CreateSetter <T, object>(field)((T)target, value);
                    structValueGetter = () => field.GetValue(source);
                    structValueSetter = (target, value) => field.SetValue(target, value);
                    break;

                default:
                    throw new ArgumentException("Only filed and property cloning supported.");
                }

                object sourceValue = null;
                if (_type.IsValueType)
                {
                    sourceValue = structValueGetter();
                }
                else
                {
                    sourceValue = valueGetter();
                }

                if (sourceValue == null)
                {
                    continue;
                }
                var clone = CloneMakerFactory.GetCloneMaker(sourceValue, member.Item3, _typeMembers, _cloned).Clone(sourceValue, member.Item3);
                if (_type.IsValueType)
                {
                    object boxed = copy;
                    structValueSetter(boxed, clone);
                    copy            = (T)boxed;
                    _cloned[source] = copy;
                }
                else
                {
                    valueSetter(copy, clone);
                }
            }
            return(copy);
        }