public void DefaultNumberFormatTest() { var data = new[] { new { TextValue = "SomeText", DateValue = DateTime.Now, DoubleValue = 10.2, IntValue = 5 } }; var dynamicProperties = new[] { DynamicProperty.Create(data, "DynamicColumn1", "Display Name 1", typeof(DateTime?), n => DateTime.Now.AddDays(n.IntValue - 4)), DynamicProperty.Create(data, "DynamicColumn2", "Display Name 2", typeof(double), n => n.DoubleValue - 0.2) }; var excelPackage = EnumerableExporter.Create(data, dynamicProperties) .DefaultNumberFormat(typeof(DateTime), "| yyyy-MM-dd") .DefaultNumberFormat(typeof(DateTime?), "|| yyyy-MM-dd") .DefaultNumberFormat(typeof(double), "0.00 $") .DefaultNumberFormat(typeof(int), "00") .CreateExcelPackage(); var excelWorksheet = excelPackage.Workbook.Worksheets.First(); //TestHelper.OpenDocument(excelPackage); string numberDecimalSeparator = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator; Assert.IsTrue(excelWorksheet.Cells[2, 2].Text == DateTime.Today.ToString("| yyyy-MM-dd")); //DateValue Assert.IsTrue(excelWorksheet.Cells[2, 3].Text == $"10{numberDecimalSeparator}20 $"); //DoubleValue Assert.IsTrue(excelWorksheet.Cells[2, 4].Text == "05"); //IntValue Assert.IsTrue(excelWorksheet.Cells[2, 5].Text == DateTime.Today.AddDays(1).ToString("|| yyyy-MM-dd")); //DynamicColumn1 Assert.IsTrue(excelWorksheet.Cells[2, 6].Text == $"10{numberDecimalSeparator}00 $"); //DynamicColumn2 }
public void Create_CalledTwiceWithPropertyInfo_ReturnsCachedProperty() { DynamicProperty one = DynamicProperty.Create(typeof(Person).GetProperty("Brother")); DynamicProperty two = DynamicProperty.Create(typeof(Person).GetProperty("Brother")); Assert.AreSame(one, two); }
public void TestDynamicProperty() { Random rand = new Random(); Person person = new Person(); person.Brother = new Person(); TimeSpan duration; DateTime start; start = DateTime.Now; for (int i = 0; i < 100000; i++) { Person brother = person.Brother; brother.Age++; } duration = DateTime.Now - start; Console.WriteLine(duration.ToString()); DynamicProperty brotherProperty = DynamicProperty.Create(typeof(Person), "Brother"); start = DateTime.Now; for (int i = 0; i < 100000; i++) { Person brother = (Person)brotherProperty.GetValue(person); brother.Age++; } duration = DateTime.Now - start; Console.WriteLine(duration.ToString()); }
public void GetValue_PropertyIsValueType_GetsBoxedAndReturnsCorrectValue() { _target = DynamicProperty.Create(typeof(Person), "Age"); _person.Age = 100; object age = _target.GetValue(_person); Assert.AreEqual(100, age); }
public object GetPropertyValue(object obj) { if (_dynamicProperty == null) { _dynamicProperty = DynamicProperty.Create(this.Property); } return(_dynamicProperty.GetValue(obj)); }
public PropertyMap(string key, PropertyInfo property, ColumnAttribute attribute) { this.Key = key; this.Property = DynamicProperty.Create(property); this.Converter = TypeDescriptor.GetConverter(this.Property.PropertyType); this.Column = attribute; if (property != null && property.DeclaringType != null) { this.TypeName = property.DeclaringType.Name; } }
public void GetValue_PropertyIsReferenceType_GetsBoxedAndReturnsCorrectValue() { Person brother = new Person(); _person.Brother = brother; _target = DynamicProperty.Create(typeof(Person), "Brother"); object brotherDynamic = _target.GetValue(_person); Assert.AreSame(brother, brotherDynamic); }
public PropertyMap(string key, PropertyInfo property) { this.Key = key; this.Property = DynamicProperty.Create(property); this.Converter = TypeDescriptor.GetConverter(this.Property.PropertyType); string[] keysSplited = key.Split(new char[] { MapConsts.SeparatorChar }, StringSplitOptions.RemoveEmptyEntries); this.KeyParts = new List <string>(); string tempKey = string.Empty; foreach (string keySplited in keysSplited) { tempKey = string.IsNullOrEmpty(tempKey) ? keySplited : string.Format("{0}{1}{2}", tempKey, MapConsts.SeparatorChar, keySplited); this.KeyParts.Add(tempKey); } }
public object GetPropertyValue(object obj) { if (_dynamicProperty == null) { if (_Expression != null) { _dynamicProperty = DynamicProperty.Create(_Expression); } else { _dynamicProperty = DynamicProperty.Create(this.Property); } } return(_dynamicProperty.GetValue(obj)); }
/// <summary> /// Initializes a new instance of the <see cref="DbProvider"/> class. /// </summary> /// <param name="dbMetadata">The db metadata.</param> public DbProvider(IDbMetadata dbMetadata) { this.dbMetadata = dbMetadata; newCommand = DynamicConstructor.Create(dbMetadata.CommandType.GetConstructor(Type.EmptyTypes)); // Oracle needs custom bind by name property set to true as it's false by default var bindByNameProperty = dbMetadata.CommandType.GetProperty("BindByName"); if (bindByNameProperty != null && bindByNameProperty.CanWrite) { commandBindByName = DynamicProperty.Create(bindByNameProperty); } newConnection = DynamicConstructor.Create(dbMetadata.ConnectionType.GetConstructor(Type.EmptyTypes)); newCommandBuilder = DynamicConstructor.Create(dbMetadata.CommandBuilderType.GetConstructor(Type.EmptyTypes)); newDataAdapter = DynamicConstructor.Create(dbMetadata.DataAdapterType.GetConstructor(Type.EmptyTypes)); newParameter = DynamicConstructor.Create(dbMetadata.ParameterType.GetConstructor(Type.EmptyTypes)); }
public void ConfigureTest() { var data = new[] { new { TextValue = "SomeText", DateValue = DateTime.Now, DoubleValue = 10.2, IntValue = 5 } }; var dynamicProperties = new[] { DynamicProperty.Create(data, "DynamicColumn1", "Display Name 1", typeof(DateTime?), n => DateTime.Now.AddDays(n.IntValue - 4)), DynamicProperty.Create(data, "DynamicColumn2", "Display Name 2", typeof(double), n => n.DoubleValue - 0.2) }; var excelPackage = EnumerableExporter.Create(data, dynamicProperties) .Configure(n => n.IntValue, configuration => { configuration.Header.Text = ""; }) .Configure(n => n.DateValue, configuration => { configuration.Header.Text = " "; configuration.Header.SetStyle = style => { style.Border.Bottom.Style = ExcelBorderStyle.Thick; }; configuration.Content.NumberFormat = "dd-MM-yyyy"; configuration.Content.SetStyle = style => { style.Border.Left.Style = ExcelBorderStyle.Dashed; style.Border.Right.Style = ExcelBorderStyle.Dashed; }; }) .Configure(new [] { "DynamicColumn1", "IntValue" }, n => { n.Header.SetStyle = style => { style.Font.Bold = true; style.Font.Color.SetColor(Color.Black); }; }) .CustomizeTable(range => { var newRange = range.Worksheet.Cells[range.End.Row, range.Start.Column, range.End.Row, range.End.Column]; newRange.Style.Fill.PatternType = ExcelFillStyle.Solid; newRange.Style.Fill.BackgroundColor.SetColor(Color.HotPink); }) .CreateExcelPackage(); TestHelper.OpenDocument(excelPackage); var excelWorksheet = excelPackage.Workbook.Worksheets.First(); //header Assert.IsTrue(excelWorksheet.Cells[1, 2].Style.Border.Bottom.Style == ExcelBorderStyle.Thick); Assert.IsTrue(excelWorksheet.Cells[1, 2].Text == " "); Assert.IsTrue(excelWorksheet.Cells[1, 4].Text == "Int Value"); Assert.IsTrue(excelWorksheet.Cells[1, 1].Style.Fill.BackgroundColor.Rgb != "FFFF69B4"); //data Assert.IsTrue(excelWorksheet.Cells[2, 2].Text == DateTime.Now.ToString("dd-MM-yyyy")); Assert.IsTrue(excelWorksheet.Cells[2, 2].Style.Border.Left.Style == ExcelBorderStyle.Dashed); Assert.IsTrue(excelWorksheet.Cells[2, 2].Style.Border.Right.Style == ExcelBorderStyle.Dashed); Assert.IsTrue(excelWorksheet.Cells[2, 1].Style.Fill.BackgroundColor.Rgb == "FFFF69B4"); }