Esempio n. 1
0
        public void CreateAccessorChain_SingleElement()
        {
            var chain = MapperUtils.CreateAccessorChain(MemberExpressions.GetExpressionChain <ClassWithSeveralPropertiesDest>(c => c.Child));

            Assert.IsNotNull(chain);
            var destination = new ClassWithSeveralPropertiesDest();
            var child       = new ChildClass();

            chain.Set(destination, child);
            Assert.AreSame(child, destination.Child);
        }
        public void GetMemberChain_DeepMemberChain_WithCast()
        {
            var memberInfo = MemberExpressions.GetExpressionChain <DeepClass>(c => ((string)c.String).Length);
            var expected   = new[]
            {
                MemberExpressions.GetMemberInfo <DeepClass>(c => c.String),
                MemberExpressions.GetMemberInfo <string>(c => c.Length)
            };

            Assert.IsNotNull(memberInfo);
            Assert.AreEqual(expected, memberInfo);
        }
Esempio n. 3
0
        public void CreateAccessorChain_MultipleElements_WithConstruction_DeeperClass()
        {
            var resourceMapper = new ResourceMapper <object>();

            resourceMapper.InitializeMap();
            var chain = MapperUtils.CreateConstructingAccessorChain <object>(MemberExpressions.GetExpressionChain <DeeperClass>(c => c.DeepClass.Child.String), resourceMapper);

            Assert.IsNotNull(chain);
            var          destination = new DeeperClass();
            const string child       = "teststring";

            chain(destination, child, null);
            Assert.AreSame(child, destination.DeepClass.Child.String);
        }
Esempio n. 4
0
        public IMappingCollection <TFrom, TTo, TContext> Set <TPropertyType, TGetterType>(
            Expression <Func <TTo, TPropertyType> > toExpression,
            Expression <Func <TFrom, TGetterType> > fromExpression,
            bool?remap = null)
        {
            var to = MemberExpressions.GetExpressionChain(toExpression); // This call should throw an error if toExpression is not a propery chain

            try
            {
                var from = MemberExpressions.GetExpressionChain(fromExpression);
                return(Set(to, typeof(TPropertyType), from, typeof(TGetterType), remap));
            }
            catch (MemberExpressionException)
            {
                var fromDelegate = fromExpression.Compile();
                return(Set(toExpression, (frm, t, context) => fromDelegate(frm), remap));
            }
        }
Esempio n. 5
0
        public IMappingCollection <TFrom, TTo, TContext> Set <TPropertyType, TGetterType>(
            Expression <Func <TTo, TPropertyType> > toExpression,
            Func <TFrom, TTo, TContext, TGetterType> getter, bool?remap = null)
        {
            var toChain = MemberExpressions.GetExpressionChain(toExpression);

            if (!toChain.Last().IsWritable())
            {
                throw new ArgumentException(string.Format("Target member {0} must be writeable", toChain));
            }
            var setter = MapEntry(toChain);

            setter.DestinationType  = typeof(TPropertyType);
            setter.SourceFunc       = (MapperAction <TContext>)((from, to, context) => getter((TFrom)from, (TTo)to, context));
            setter.SourceType       = typeof(TGetterType);
            setter.SourceObjectType = MemberEntryType.Function;
            setter.Remap            = remap ?? RequiresRemappingByDefault(typeof(TPropertyType), typeof(TGetterType), true);
            if (!setter.Remap)
            {
                VerifyReturnType(typeof(TPropertyType), typeof(TGetterType));
            }
            return(this);
        }
Esempio n. 6
0
 private static MemberInfo GetSrcInfo(Expression <Func <ClassWithSeveralPropertiesSrcNullable, object> > expression)
 {
     return(MemberExpressions.GetExpressionChain(expression).First());
 }
Esempio n. 7
0
 private static MemberInfo[] GetAllDestInfo(Expression <Func <ClassWithSeveralPropertiesDest, object> > expression)
 {
     return(MemberExpressions.GetExpressionChain(expression));
 }
 public void GetExpressionChain_NonPropertyOrFieldType_ThrowsException()
 {
     Assert.Throws <MemberExpressionException>(() => MemberExpressions.GetExpressionChain <DeepClass>(c => c.String.Length + 1));
 }
 public void GetExpressionChain_NonGeneric_NullMember_ThrowsException()
 {
     Assert.Throws <ArgumentNullException>(() => MemberExpressions.GetExpressionChain(null));
 }