コード例 #1
0
        public void GetComponentType()
        {
            var component  = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.Equal(component.GetType(), propertyDescriptor.ComponentType);
        }
コード例 #2
0
        public void GetComponentType()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.Equal(component.GetType(), propertyDescriptor.ComponentType);
        }
コード例 #3
0
        public void CopyConstructorAddsAttribute()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor oldPropertyDescriptor = properties.Find(nameof(component.Property), false);
            var newAttribute = new DescriptorTestAttribute();

            PropertyDescriptor newPropertyDescriptor = TypeDescriptor.CreateProperty(component.GetType(), oldPropertyDescriptor, newAttribute);

            Assert.True(newPropertyDescriptor.Attributes.Contains(newAttribute));
        }
コード例 #4
0
        public void CopyConstructorAddsAttribute()
        {
            var component  = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor oldPropertyDescriptor = properties.Find(nameof(component.Property), false);
            var newAttribute = new DescriptorTestAttribute();

            PropertyDescriptor newPropertyDescriptor = TypeDescriptor.CreateProperty(component.GetType(), oldPropertyDescriptor, newAttribute);

            Assert.True(newPropertyDescriptor.Attributes.Contains(newAttribute));
        }
コード例 #5
0
        public void GetValueDoesNotHandleExceptions()
        {
            var component  = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.PropertyWhichThrows), false);

            Assert.Throws <TargetInvocationException>(() => propertyDescriptor.GetValue(component));
        }
コード例 #6
0
        public void ResetValueReturnsFalseWhenValueEqualsDefault()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.False(propertyDescriptor.CanResetValue(component));
        }
コード例 #7
0
        public void ShouldSerializeValueReturnsTrueWhenValueIsNotDefault()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue - 1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.True(propertyDescriptor.ShouldSerializeValue(component));
        }
コード例 #8
0
        public void ResetValueReturnsFalseWhenValueEqualsDefault()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.False(propertyDescriptor.CanResetValue(component));
        }
コード例 #9
0
        public void ShouldSerializeValueHandlesEnumWithBackingTypeDefaultValue()
        {
            var component = new DescriptorTestComponent();

            component.EnumProperty = DescriptorTestEnum.Value1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.EnumProperty), false);

            Assert.True(propertyDescriptor.ShouldSerializeValue(component));
        }
コード例 #10
0
ファイル: TypeDescriptorTests.cs プロジェクト: ESgarbi/corefx
        public void AddAttribute()
        {
            var component = new DescriptorTestComponent();
            var addedAttribute = new DescriptorTestAttribute("expected string");

            TypeDescriptor.AddAttributes(component.GetType(), addedAttribute);

            AttributeCollection attributes = TypeDescriptor.GetAttributes(component);
            Assert.True(attributes.Contains(addedAttribute));
        }
コード例 #11
0
        public void GetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = 37;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            var retrievedValue = propertyDescriptor.GetValue(component);

            Assert.Equal(component.Property, retrievedValue);
        }
コード例 #12
0
        public void AddAttribute()
        {
            var component      = new DescriptorTestComponent();
            var addedAttribute = new DescriptorTestAttribute("expected string");

            TypeDescriptor.AddAttributes(component.GetType(), addedAttribute);

            AttributeCollection attributes = TypeDescriptor.GetAttributes(component);

            Assert.True(attributes.Contains(addedAttribute));
        }
コード例 #13
0
        public void RemoveAddedEventHandler()
        {
            var             component       = new DescriptorTestComponent();
            Action          eventHandler    = () => Assert.True(false, "EventDescriptor failed to remove an event handler");
            EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            eventDescriptor.RemoveEventHandler(component, eventHandler);

            // component.Event should now have no handler => raising it should throw a NullReferenceException
            Assert.Throws <NullReferenceException>(() => component.RaiseEvent());
        }
コード例 #14
0
        public void RemoveAddedEventHandler()
        {
            var component = new DescriptorTestComponent();
            Action eventHandler = () => Assert.True(false, "EventDescriptor failed to remove an event handler");
            EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            eventDescriptor.RemoveEventHandler(component, eventHandler);

            // component.Event should now have no handler => raising it should throw a NullReferenceException
            Assert.Throws(typeof(NullReferenceException), () => component.RaiseEvent());
        }
コード例 #15
0
        public void ResetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue - 1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            // this should set the property's value to that provided by the DefaultValueAttribute
            propertyDescriptor.ResetValue(component);

            Assert.Equal(DescriptorTestComponent.DefaultPropertyValue, component.Property);
        }
コード例 #16
0
        public void RaiseAddedEventHandler()
        {
            var             component             = new DescriptorTestComponent();
            var             eventHandlerWasCalled = false;
            Action          eventHandler          = () => eventHandlerWasCalled = true;
            var             eventInfo             = typeof(DescriptorTestComponent).GetEvent(nameof(DescriptorTestComponent.ActionEvent));
            EventDescriptor eventDescriptor       = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            component.RaiseEvent();

            Assert.True(eventHandlerWasCalled);
        }
コード例 #17
0
        public void RaiseAddedEventHandler()
        {
            var component = new DescriptorTestComponent();
            var eventHandlerWasCalled = false;
            Action eventHandler = () => eventHandlerWasCalled = true;
            var eventInfo = typeof(DescriptorTestComponent).GetTypeInfo().GetEvent(nameof(DescriptorTestComponent.ActionEvent));
            EventDescriptor eventDescriptor = TypeDescriptor.CreateEvent(component.GetType(), nameof(component.ActionEvent), typeof(Action));

            eventDescriptor.AddEventHandler(component, eventHandler);
            component.RaiseEvent();

            Assert.True(eventHandlerWasCalled);
        }
コード例 #18
0
        public void RaiseAddedValueChangedHandler()
        {
            var component  = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);
            var          handlerWasCalled         = false;
            EventHandler valueChangedHandler      = (_, __) => handlerWasCalled = true;

            propertyDescriptor.AddValueChanged(component, valueChangedHandler);
            propertyDescriptor.SetValue(component, int.MaxValue);

            Assert.True(handlerWasCalled);
        }
コード例 #19
0
        public void RaiseAddedValueChangedHandler()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);
            var handlerWasCalled = false;
            EventHandler valueChangedHandler = (_, __) => handlerWasCalled = true;

            propertyDescriptor.AddValueChanged(component, valueChangedHandler);
            propertyDescriptor.SetValue(component, int.MaxValue);

            Assert.True(handlerWasCalled);
        }
コード例 #20
0
        public void GetValueDoesNotHandleExceptions()
        {
            var component = new DescriptorTestComponent();
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.PropertyWhichThrows), false);

            Assert.ThrowsAny<Exception>(() => propertyDescriptor.GetValue(component));
        }
コード例 #21
0
        public void ResetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue - 1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            // this should set the property's value to that provided by the DefaultValueAttribute
            propertyDescriptor.ResetValue(component);

            Assert.Equal(DescriptorTestComponent.DefaultPropertyValue, component.Property);
        }
コード例 #22
0
        public void GetValue()
        {
            var component = new DescriptorTestComponent();
            component.Property = 37;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            var retrievedValue = propertyDescriptor.GetValue(component);

            Assert.Equal(component.Property, retrievedValue);
        }
コード例 #23
0
        public void ShouldSerializeValueReturnsTrueWhenValueIsNotDefault()
        {
            var component = new DescriptorTestComponent();
            component.Property = DescriptorTestComponent.DefaultPropertyValue - 1;
            var properties = TypeDescriptor.GetProperties(component.GetType());
            PropertyDescriptor propertyDescriptor = properties.Find(nameof(component.Property), false);

            Assert.True(propertyDescriptor.ShouldSerializeValue(component));
        }