Esempio n. 1
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 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);
        }
        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);
        }
 public override void ApplyProperty(IComponent component, PropertySetterInfo propertySetterInfo)
 {
     if (!propertySetter.SetValue(component, propertySetterInfo.PropertyName, propertySetterInfo.Value))
     {
         throw new PropertyNotFoundException("Invalid property assignement!", propertySetterInfo.Line, propertySetterInfo.Source);
     }
 }
Esempio n. 5
0
        public void PropertySetter_SetTimeSpan(float seconds)
        {
            var applicationOptions = OptionsMocks.CreateApplicationOptions();
            var propertySetter     = new PropertySetter(applicationOptions);
            var component          = ComponentMocks.CreateComponent <BasicTemplatelessComponent>();

            component.Name = "TestComponent";

            var propertyValue = new TimeSpanPropertyValue(seconds);

            propertySetter.SetValue(component, nameof(BasicTemplatelessComponent.TimeSpan), propertyValue);

            Assert.Equal(TimeSpan.FromSeconds(seconds), component.TimeSpan);
        }
Esempio n. 6
0
        public void PropertySetter_SetScalar(float value)
        {
            var applicationOptions = OptionsMocks.CreateApplicationOptions();
            var propertySetter     = new PropertySetter(applicationOptions);
            var component          = ComponentMocks.CreateComponent <BasicTemplatelessComponent>();

            component.Name = "TestComponent";

            var propertyValue = new ScalarPropertyValue(value);

            propertySetter.SetValue(component, nameof(BasicTemplatelessComponent.Length), propertyValue);

            Assert.Equal(value, component.Single);
        }
Esempio n. 7
0
        public void PropertySetter_SetLength(string unit, float length)
        {
            var applicationOptions = OptionsMocks.CreateApplicationOptions();
            var propertySetter     = new PropertySetter(applicationOptions);
            var component          = ComponentMocks.CreateComponent <BasicTemplatelessComponent>();

            component.Name = "TestComponent";

            var propertyValue = new LengthPropertyValue(unit, length);

            propertySetter.SetValue(component, nameof(BasicTemplatelessComponent.Length), propertyValue);

            Assert.Equal(unit == "%", component.Length[UnitType.Percentage] != 0);
            Assert.Equal(unit == "px", component.Length[UnitType.Pixel] != 0);
            Assert.Equal(unit == "x", component.Length[UnitType.Ratio] != 0);
            Assert.Equal(unit == "u", component.Length[UnitType.Unit] != 0);
        }
Esempio n. 8
0
 public override void AddAttribute(string name, string value)
 {
     if (name == "name")
     {
         component = ComponentGetter.GetFromAssemblies(value, element.module.data.GetNamespaces(), element.data.GetGameObject());
         if (component == null)
         {
             throw new SetAttributeException(name, value, this.name, value + " is not found. Ensure you wrote it correct or added \"using namespace\" element to the xml module.");
         }
     }
     else if (component != null)
     {
         PropertySetter.SetValue(component, name, value, PropertySetter.Data.Create(element.data, element.module.data));
     }
     else
     {
         throw new SetAttributeException(name, value, this.name, "Set name attribute first.");
     }
 }