コード例 #1
0
        private static int GetPropertyId <T>(IEnumerable <Property> studentProperties, Expression <Func <T> > expression)
        {
            string entityName   = ((MemberExpression)expression.Body).Member.ReflectedType.FullName;
            string propertyName = PropertyReflection.GetMemberName <T>(expression);

            return(studentProperties.Where(p => p.EntityName.Equals(entityName) && p.Name.Equals(propertyName)).Select(p => p.Id).SingleOrDefault());
        }
コード例 #2
0
        public void ShouldReturnFalseIfPropertyNotExists()
        {
            var dummyObject = new DummyObject();
            var propertyReflection = new PropertyReflection();
            var result = propertyReflection.HasProperty(dummyObject, "FooBar");

            Assert.AreEqual(false, result);
        }
コード例 #3
0
        public void ShouldReturnTrueIfPropertyExists()
        {
            var dummyObject = new DummyObject();
            var propertyReflection = new PropertyReflection();
            var result = propertyReflection.HasProperty(dummyObject, "DateTime");

            Assert.AreEqual(true, result);
        }
コード例 #4
0
        public void ShouldReturnNullWhenGetPropertyValueByRecursionFoundNoProperty()
        {
            var dummyObject = new EmptyDummy();
            var propertyReflection = new PropertyReflection();
            var result = propertyReflection.GetPropertyValue(dummyObject, "Doughnut", true);

            Assert.IsNull(result);
        }
コード例 #5
0
        public void ShouldGetPropertyValueSuccessfullyByRecursion()
        {
            var dummyObject = new DummyObjectComplex { Dummy = new DummyObject { Name = "Stars" } };
            var propertyReflection = new PropertyReflection();
            var result = propertyReflection.GetPropertyValue(dummyObject, "Name", true);

            Assert.IsNotNull(result);
            Assert.AreEqual("Stars", result.ToString());
        }
コード例 #6
0
        public void ShouldGetPropertyValueSuccessfully()
        {
            var dummyObject = new DummyObject { Name = "Stars" };
            var propertyReflection = new PropertyReflection();
            var result = propertyReflection.GetPropertyValue(dummyObject, "Name");

            Assert.IsNotNull(result);
            Assert.AreEqual(result, "Stars");
        }
コード例 #7
0
        public void ShouldGetPropertyValueSuccessfullyByRecursionOnParentObject()
        {
            var dummyObject = new DummyObject { Id = 5 };
            var propertyReflection = new PropertyReflection();
            var result = propertyReflection.GetPropertyValue(dummyObject, "Id", true);

            Assert.IsNotNull(result);
            Assert.AreEqual(5, Convert.ToInt32(result));
        }
コード例 #8
0
        public void UpdateNestedIntProperty()
        {
            //Arrange
            var person = CreatSut();

            //Act
            PropertyReflection.SetProperty("AddressDetail.Number", person, 4);
            //Assert
            Assert.Equal(4, person.Address.Number);
        }
コード例 #9
0
        public void UpdateSingularIntProperty()
        {
            //Arrange
            var person = CreatSut();

            //Act
            PropertyReflection.SetProperty("Age", person, 25);
            //Assert
            Assert.Equal(25, person.Age);
        }
コード例 #10
0
        public void UpdateNestedStringProperty()
        {
            //Arrange
            var person = CreatSut();

            //Act
            PropertyReflection.SetProperty("AddressDetail.CountryDetail.CountryName", person, "SWEDEN");
            //Assert
            Assert.Equal("SWEDEN", person.Address.Country.CountryName);
        }
コード例 #11
0
        public void GetPropertyValue()
        {
            //Arrange
            var person = CreatSut();
            //Act
            var result = PropertyReflection.GetPropertyValue <Person>(person,
                                                                      x => x.Address.Country.CountryName);

            //Assert
            Assert.Equal("USA", result);
        }
コード例 #12
0
        public void GetPropertyDisplayName()
        {
            //Arrange
            var person = CreatSut();
            //Act
            var result = PropertyReflection
                         .GetPropertyDisplayName <Person>(x => x.Address.Country.CountryName);

            //Assert
            Assert.Equal("AddressDetail.CountryDetail.CountryName", result);
        }
コード例 #13
0
        public void ShouldThrowExceptionWhenPropertyNotExistingOnGettingValue()
        {
            var dummyObject = new DummyObject();
            var propertyReflection = new PropertyReflection();
            var result = Assert.Throws<Exception>(() =>
                propertyReflection.GetPropertyValue(dummyObject, "Doughnut"));

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Exception), result);
            Assert.AreEqual("Property Doughnut does not exist on DummyObject object", result.Message);
        }
コード例 #14
0
        // Static - Property Getter
        public static Expression <Func <TProperty> > StaticPropertyGetter <TClass, TProperty>(string propertyName)
        {
            Contract.Requires(propertyName.SafeHasContent());

            var type = typeof(TClass);
            var propertyExpression = default(Expression);
            var propertyNameSplit  = propertyName.Split('.');

            foreach (var name in propertyNameSplit)
            {
                var propertyInfo     = TypeReflection.GetProperty(type, name, ReflectionFlags.Public | ReflectionFlags.Static | ReflectionFlags.Instance);
                var isPropertyStatic = PropertyReflection.IsStatic(propertyInfo);
                propertyExpression = Expression.Property(isPropertyStatic ? null : propertyExpression, propertyInfo);
                type = propertyInfo.PropertyType;
            }

            var lambdaExpression = Expression.Lambda <Func <TProperty> >(propertyExpression);

            return(lambdaExpression);
        }
コード例 #15
0
        // Instance - Property Getter
        public static Expression <Func <TObject, TProperty> > PropertyGetter <TObject, TProperty>(string propertyName)
        {
            Contract.Requires(propertyName.SafeHasContent());

            var type = typeof(TObject);
            var instanceExpression = Expression.Parameter(type, "x");
            var propertyExpression = (Expression)instanceExpression;
            var propertyNameSplit  = propertyName.Split('.');

            foreach (var name in propertyNameSplit)
            {
                var propertyInfo     = TypeReflection.GetProperty(type, name, ReflectionFlags.Public | ReflectionFlags.Instance | ReflectionFlags.Static);
                var isPropertyStatic = PropertyReflection.IsStatic(propertyInfo);
                propertyExpression = Expression.Property(!isPropertyStatic ? propertyExpression : null, propertyInfo);
                type = propertyInfo.PropertyType;
            }

            var lambdaExpression = Expression.Lambda <Func <TObject, TProperty> >(propertyExpression, instanceExpression);

            return(lambdaExpression);
        }
コード例 #16
0
ファイル: CSV.cs プロジェクト: mav01981/Helper.Library
        private DataTable ConvertToDataTable <T>(IEnumerable <T> data,
                                                 params Expression <Func <T, object> >[] columnsFunc)
        {
            DataTable table = new DataTable();

            foreach (var column in columnsFunc)
            {
                string columnName = PropertyReflection.GetPropertyDisplayName <T>(column);
                table.Columns.Add(columnName);
            }

            foreach (T obj in data)
            {
                DataRow row = table.NewRow();

                for (int i = 0; i < table.Columns.Count; i++)
                {
                    row[table.Columns[i].ColumnName] = PropertyReflection.GetPropertyValue <T>(obj, columnsFunc[i]);
                }
                table.Rows.Add(row);
            }
            return(table);
        }
コード例 #17
0
        public void ShouldSetPropertySuccessfully()
        {
            var dummyObject = new DummyObject();
            var propertyReflection = new PropertyReflection();

            Assert.DoesNotThrow(() => propertyReflection.SetProperty(dummyObject, "Name", "FooBar"));
            Assert.AreEqual(dummyObject.Name, "FooBar");
        }