public void PropertySetter2WorksWithStatic()
        {
            Baz.Qux = -1;

            var property = typeof(Baz).GetProperty(nameof(Baz.Qux));

            var setter = new PropertySetter <Foo, int>(property);

            setter.Action.Target.Should().BeSameAs(setter);
            setter.Action.Method.Name.Should().Be(nameof(setter.SetValueReflection));
            setter.Action.Method.DeclaringType.Should().Be(typeof(PropertySetter <Foo, int>));

            var reflectionTimer = Stopwatch.StartNew();

            setter.SetValue(null, 123);
            reflectionTimer.Stop();

            Baz.Qux.Should().Be(123);

            setter.SetOptimizedAction();

            setter.Action.Target.Should().NotBeSameAs(setter);
            setter.Action.Method.Name.Should().Be(PropertySetter.SetValueOptimized);
            setter.Action.Method.DeclaringType.Should().NotBe(typeof(PropertySetter <Foo, int>));

            var optimizedTimer = Stopwatch.StartNew();

            setter.SetValue(null, 456);
            optimizedTimer.Stop();

            Baz.Qux.Should().Be(456);

            optimizedTimer.Elapsed.Should().BeLessThan(reflectionTimer.Elapsed);
        }
Example #2
0
        public void PropertySetter2Works()
        {
            var foo = new Foo();

            var setter = new PropertySetter <Foo, int>(_property);

            setter.Action.Target.Should().BeSameAs(setter);
            setter.Action.Method.Name.Should().Be(nameof(setter.SetValueReflection));
            setter.Action.Method.DeclaringType.Should().Be(typeof(PropertySetter <Foo, int>));

            using (var gc = new GCNoRegion(4194304))
            {
                var reflectionTimer = Stopwatch.StartNew();
                setter.SetValue(foo, 123);
                reflectionTimer.Stop();

                foo.Bar.Should().Be(123);

                setter.SetOptimizedAction();

                setter.Action.Target.Should().NotBeSameAs(setter);
                setter.Action.Method.Name.Should().Be(PropertySetter.SetValueOptimized);
                setter.Action.Method.DeclaringType.Should().NotBe(typeof(PropertySetter <Foo, int>));

                var optimizedTimer = Stopwatch.StartNew();
                setter.SetValue(foo, 456);
                optimizedTimer.Stop();

                foo.Bar.Should().Be(456);

                optimizedTimer.Elapsed.Should().BeLessThan(reflectionTimer.Elapsed);
            }
        }
        public void PropertySetterWorks()
        {
            var foo = new Foo();

            var setter = new PropertySetter(_property);

            setter.Action.Target.Should().BeSameAs(_property);
            setter.Action.Method.Name.Should().Be(nameof(_property.SetValue));
            setter.Action.Method.DeclaringType.Should().Be(typeof(PropertyInfo));

            var reflectionTimer = Stopwatch.StartNew();

            setter.SetValue(foo, 123);
            reflectionTimer.Stop();

            foo.Bar.Should().Be(123);

            setter.SetOptimizedAction();

            setter.Action.Target.Should().NotBeSameAs(_property);
            setter.Action.Method.Name.Should().Be(PropertySetter.SetValueOptimized);
            setter.Action.Method.DeclaringType.Should().NotBe(typeof(PropertyInfo));

            var optimizedTimer = Stopwatch.StartNew();

            setter.SetValue(foo, 456);
            optimizedTimer.Stop();

            foo.Bar.Should().Be(456);

            optimizedTimer.Elapsed.Should().BeLessThan(reflectionTimer.Elapsed);
        }