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()); }
public void ShouldReturnFalseIfPropertyNotExists() { var dummyObject = new DummyObject(); var propertyReflection = new PropertyReflection(); var result = propertyReflection.HasProperty(dummyObject, "FooBar"); Assert.AreEqual(false, result); }
public void ShouldReturnTrueIfPropertyExists() { var dummyObject = new DummyObject(); var propertyReflection = new PropertyReflection(); var result = propertyReflection.HasProperty(dummyObject, "DateTime"); Assert.AreEqual(true, result); }
public void ShouldReturnNullWhenGetPropertyValueByRecursionFoundNoProperty() { var dummyObject = new EmptyDummy(); var propertyReflection = new PropertyReflection(); var result = propertyReflection.GetPropertyValue(dummyObject, "Doughnut", true); Assert.IsNull(result); }
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()); }
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"); }
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)); }
public void UpdateNestedIntProperty() { //Arrange var person = CreatSut(); //Act PropertyReflection.SetProperty("AddressDetail.Number", person, 4); //Assert Assert.Equal(4, person.Address.Number); }
public void UpdateSingularIntProperty() { //Arrange var person = CreatSut(); //Act PropertyReflection.SetProperty("Age", person, 25); //Assert Assert.Equal(25, person.Age); }
public void UpdateNestedStringProperty() { //Arrange var person = CreatSut(); //Act PropertyReflection.SetProperty("AddressDetail.CountryDetail.CountryName", person, "SWEDEN"); //Assert Assert.Equal("SWEDEN", person.Address.Country.CountryName); }
public void GetPropertyValue() { //Arrange var person = CreatSut(); //Act var result = PropertyReflection.GetPropertyValue <Person>(person, x => x.Address.Country.CountryName); //Assert Assert.Equal("USA", result); }
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); }
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); }
// 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); }
// 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); }
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); }
public void ShouldSetPropertySuccessfully() { var dummyObject = new DummyObject(); var propertyReflection = new PropertyReflection(); Assert.DoesNotThrow(() => propertyReflection.SetProperty(dummyObject, "Name", "FooBar")); Assert.AreEqual(dummyObject.Name, "FooBar"); }