public void GetGetters_WhenTypePassedIn_ExpectWorkingGetters() { // Arrange var mockPropertyInfoStore = new Mock<IPropertyInfoStore>(); var propertGetSetters = new PropertyGetSettersTyped<GetSetterTestEntity>(mockPropertyInfoStore.Object); mockPropertyInfoStore.Setup(r => r.GetDeclaredProperties(It.IsAny<Type>())) .Returns(typeof(GetSetterTestEntity).GetProperties()); var testEntity = new GetSetterTestEntity { Int1 = 1, String1 = "string" }; var propertyDictionary = new Dictionary<string, object>(); // Act var stopwatch = Stopwatch.StartNew(); var enumerable = propertGetSetters.GetGetters(typeof(GetSetterTestEntity)); foreach (var func in enumerable) { propertyDictionary.Add(func.Key, func.Value(testEntity)); } stopwatch.Stop(); // Assert this.WriteTimeElapsed(stopwatch); Assert.That(propertyDictionary["Int1"], Is.EqualTo(1)); Assert.That(propertyDictionary["String1"], Is.EqualTo("string")); }
public void GetSetters_PerformanceTest() { // Arrange const int TimesToRun = 1000000; var mockPropertyInfoStore = new Mock<IPropertyInfoStore>(); var propertGetSetters = new PropertyGetSettersTyped<GetSetterTestEntity>(mockPropertyInfoStore.Object); mockPropertyInfoStore.Setup(r => r.GetDeclaredProperties(It.IsAny<Type>())) .Returns(typeof(GetSetterTestEntity).GetProperties()); var testEntity = new GetSetterTestEntity(); var propertyDictionary = new Dictionary<string, object> { {"Int1", 1 }, {"String1", "string"} }; // Act and Assert var setters = propertGetSetters.GetSetters(typeof(GetSetterTestEntity)); var stopwatch = Stopwatch.StartNew(); for (var i = 0; i < TimesToRun; i++) { foreach (var setter in setters) { setter.Value.Setter(testEntity, propertyDictionary[setter.Key]); } } stopwatch.Stop(); Console.WriteLine($"Using Reflection: {stopwatch.ElapsedMilliseconds}ms"); stopwatch.Restart(); for (var i = 0; i < TimesToRun; i++) { testEntity.Int1 = (int)propertyDictionary["Int1"]; testEntity.String1 = (string)propertyDictionary["String1"]; } stopwatch.Stop(); Console.WriteLine($"Using Standard: {stopwatch.ElapsedMilliseconds}ms"); Console.WriteLine("\r\n-----------------------------\r\n"); }
public void GetSetters_WhenTypePassedIn_ExpectWorkingSetters() { // Arrange var mockPropertyInfoStore = new Mock<IPropertyInfoStore>(); var propertGetSetters = new PropertyGetSettersTyped<GetSetterTestEntity>(mockPropertyInfoStore.Object); mockPropertyInfoStore.Setup(r => r.GetDeclaredProperties(It.IsAny<Type>())) .Returns(typeof(GetSetterTestEntity).GetProperties()); var testEntity = new GetSetterTestEntity(); var propertyDictionary = new Dictionary<string, object> { {"Int1", 1 }, {"String1", "string"} }; // Act var stopwatch = Stopwatch.StartNew(); var setters = propertGetSetters.GetSetters(typeof(GetSetterTestEntity)); foreach (var setter in setters) { setter.Value.Setter(testEntity, propertyDictionary[setter.Key]); } stopwatch.Stop(); // Assert this.WriteTimeElapsed(stopwatch); Assert.That(testEntity.Int1, Is.EqualTo(1)); Assert.That(testEntity.String1, Is.EqualTo("string")); }