Exemplo n.º 1
0
        public void GetSetValue()
        {
            Employee person = new Employee {
                FirstName = "Franz",
                LastName  = "Huber",
                Address   = new Address {
                    City = "NY"
                }
            };

            var path1 = PropertyPath.Create <Person, string>(p => p.FirstName);

            Assert.AreEqual("Franz", path1.GetValue(person));
            path1.SetValue(person, "Hans");
            Assert.AreEqual("Hans", person.FirstName);

            var path2 = PropertyPath.Create <Employee, string>(p => p.LastName);

            Assert.AreEqual("Huber", path2.GetValue(person));
            path2.SetValue(person, "Maier");
            Assert.AreEqual("Maier", person.LastName);

            var path3 = PropertyPath.Create <Employee, string>(p => p.Address.City);

            Assert.AreEqual("NY", path3.GetValue(person));
            path3.SetValue(person, "AZ");
            Assert.AreEqual("AZ", person.Address.City);

            person.Address = null;
            AssertHelper.Throws <NullReferenceException>(() =>
                                                         path3.GetValue(person)
                                                         )
            .Containing("'[Employee].Address'")
            .Containing("'[Employee].Address.City'");

            var path4 = PropertyPath.CreateWithDefaultValue <Employee, string>(p => p.Address.City, "-");

            Assert.AreEqual("-", path4.GetValue(person));

            // SetValue should be ignored...
            path4.SetValue(person, "Test");
            Assert.AreEqual(null, person.Address);

            var path5 = PropertyPath.Create <Person, Person>(p => p);

            Assert.AreEqual(person, path5.GetValue(person));
            AssertHelper.Throws <InvalidOperationException>(
                () => path5.SetValue(person, person)
                ).Containing("empty");

            path5 = PropertyPath.Empty <Person>();
            Assert.AreEqual(person, path5.GetValue(person));
            AssertHelper.Throws <InvalidOperationException>(
                () => path5.SetValue(person, person)
                ).Containing("empty");
        }
Exemplo n.º 2
0
        public IValueAccessorBehavior <TValue> CreateMappedAccessor <TValue>(
            Expression <Func <TSourceObject, TValue> > valueSelector
            )
        {
            var path = PropertyPath.Concat(
                _sourceObjectPath,
                PropertyPath.CreateWithDefaultValue(valueSelector)
                );

            return(new MappedValueAccessorBehavior <TOwnerVM, TValue>(path));
        }